On my website I have a table with some "add" links. By clicking on the add links, a ajax request is fired and - as soon as this request has been performed - a new table row directly below the "add" link row is created.
When clicking only one "add" link at a time, the script is working as expected.
However the script called by the get request has an execution time of approximately 2 seconds. So it might happen, that requests run parallel.
In case I am clicking on the second "add" link while the first one is still loading, the two new rows both are added below the second table row. On the other way, when clicking the first "add" link while the second one is still loading, both new rows are added below the first table row. (so in between the two add links).
This is not the behaviour I wanted to have: I am expecting, that when clicking both "add" links I am getting a new row after each of the rows.
I guess this might happen, because the variable "click" is overwritten when loading the second response while the first response is still running? How can I prevent this behaviour?
$('a.open').bind('click', function () {
id=$(this).attr('id');
click=$(this);
$.get("#", {key:id})
.done(function( data ) {
text=data.html;
click.closest('tr').after('<tr><td></td><td>'+text+'</td></tr>');
})
.fail(function (jqXHR, textStatus) {
click.closest('tr').after('<tr><td></td><td>error</td></tr>');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border=1>
<tr>
<td><a href="#" id="12333" class="open">add</a></td>
<td>some text</td>
</tr>
<tr>
<td><a href="#" id="23457" class="open">add</a></td>
<td>some other text</td>
</tr>
</table>