I would like to make sure there is only one AJAX request going on (for a user) at a time. This is what I tried:
var ajaxFinished = 1;
$('#selector').keyup(function() {
if (ajaxFinished == 1) {
ajaxFinished = 0;
$.get('test.php', { par : "par" }, function(data) {
// do stuff with data
ajaxFinished = 1;
});
}
});
But the problem is the AJAX request gets executed only once now even though I activate the event multiple times.
I think Corey has it nailed. Another possibility: Could it be that the code block you quote is inside another function we don’t see? Then it could be that ajaxFinished is out of scope – correct me if I’m wrong, my knowledge of JS scoping and anonymous functions is not perfect. In that case, you would have to remove the “var” or declare it in the body.