I am having trouble phrasing my question correctly, but I have included the code below. Essentially, I want to make an ajax request, check the callback, and continue to perform the ajax request until it receives the desired response (in this case, connectedvoter equals 1).
The issue I'm facing is that the process happens very quickly, taking only about 80ms, and the number of xhr's increases rapidly at this speed. I have tried various methods to 'pause' the process, but all options I could think of seem to consume a lot of CPU.
Is there a way to slow down the frequency of requests made, perhaps to once every second or two, without using up too much 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);
} 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();