Not sure if I’m phrasing my question right, but I have the code below. Basically want to make an ajax request, check the call back, and re-perform the ajax until it gets the desired response (in the example connectedvoter==1).
The problem is it only takes like 80ms and the amount of xhr’s gets to huge numbers really fast at that speed. I tried to come up with a way to ‘pause’ but everything I could think of ate up cpu.
Is there a way to ‘slow down’ the amount of requests made, to say once a second or two without eating up cpu?
var connctedvoter = 0;
var govoters = function () {
$.ajaxSetup({
async: false
});
var url = "getconnectedvoter.php";
var data = {
userid: userid
};
$.getJSON(url, data, callback);
};
var pausevoters = function () {
console.log("pausing ajax voters");
};
var callback = function (response) {
if (response.error) {
return;
}
if (response.connectedvoter == 0) {
setTimeout(govoters, 150);
//govoters();
} else {
$('#vanid').html(response.vanid);
$('#name').html(response.name);
$("#mapurl").attr("src", response.mapurl);
$('.call').fadeIn();
return;
}
};
//DO THIS TO START
setTimeout(govoters, 150);
pausevoters();
Just increase the delay of the timeout:
Instead of 10 requests for a second:
Note that for start you can change from this:
To this:
There isn’t really a need for a timeout here.