quoting MDC :
If it is likely that your logic may take longer than the interval time, it is recommended that you call the named function recursively using window.setTimeout. For example, when using setInterval to poll a remote server every 5 seconds, network latency, a non-responding server, and many other problems can prevent the request from completing at its appointed time. That way, you might end up in a queue with XHR requests that don't necessarily return in order.For such cases, the recursive setTimeout pattern is preferred:
If it is likely that your logic may take longer than the interval time, it is recommended that you call the named function recursively using window.setTimeout. For example, when using setInterval to poll a remote server every 5 seconds, network latency, a non-responding server, and many other problems can prevent the request from completing at its appointed time. That way, you might end up in a queue with XHR requests that don't necessarily return in order.
For such cases, the recursive setTimeout pattern is preferred:
(function loop(){ setTimeout(function(){ // logic here // recurse loop(); }, 1000); })();
. setTimeout , . , , .
, . loop() XHR, setTimeout?
setInterval(): , AJAX ( ). , .
setInterval()
, setTimeout() , (.. ).
setTimeout()
, : setTimeout() ( loop()) setInterval(). loop() AJAX, .
loop()
, XHR, .
, . setInterval(), XHR- .
, - /, , A , , B - , " XHR-" " ".
, , setInterval(), "" XHR- , , setTimeout(), "" .