I have this code.
var centerTimer = 0;
function startCenterRefresh( refreshTime,position,params ) {
centerTimer = setInterval(function() {
jQuery.ajax({
type: "POST",
url: host+"/forms/refresh-center.php",
beforeSend:function(){ stopCenterRefresh(); },
success: function(){console.log(1);},
complete:function(){startCenterRefresh(refreshTime,position,params);}
});
},refreshTime);
}
function stopCenterRefresh() {
clearInterval( centerTimer );
}
Is this the proper way to pause a setInterval on a beforeSend on an ajax request then resume the setInterval after the ajax request is complete?
I’m not sure if I met my expectation on this execution.
Your idea would be greatly appreciated and rewarded.
Thanks!
clearIntervaldoesn’t pause the periodic call to a function : it removes it. So you’re totally stopping it at the first call. It’s thus ineffective.What you probably should do is simply requeue (using
setTimeout) the ajax call in the success callback.For example :