I several divs on my site, I'd like to update one by one. In order not to spam the server with 200+ requests at once, I'd like to delay them by 1s each.
What I tried:
var $tourBox = $('.tour-box');
$tourBox.each(function () {
var $box = $(this);
setTimeout(function () {
getUpdate($box);
}, 1000);
});
The update function:
function getUpdate($box) {
var $so = $box.attr('data-so');
var $url = $('#ajax-route-object-status').val();
$.get($url, {
so: $so
}).success(function (data) {
var $bg = 'bg-gray';
if (data.extra.isStarted === true && data.extra.isFinished === false) {
$bg = 'bg-orange'
}
if (data.extra.isStarted === true && data.extra.isFinished === true) {
$bg = 'bg-green'
}
if (data.extra.isLate === true && data.extra.isFinished === false) {
$bg = 'bg-red'
}
$box.removeClass('bg-gray').removeClass('bg-green').removeClass('bg-orange').removeClass('bg-red').addClass($bg);
});
}
In Chrome Dev--> Network it shows the all loaded as pending and then loads them one by one, but w/out the delay:
As you can see between 3907 and 3940, there is merely half a second delay. This doesn't change even if I have a timeout of 5000.