Suppose I have setInterval(PostToServer, 1000);
. PostToServer
function makes ajax post, which may take longer than a second. So what happens then: a second call is made while the first hasn't finished or the end of call is awaited before making new one?
The calls overlap.
setInterval
ensures that the functions are run regularly, without waiting for the previous result.
If you want to await the response, change the interval method to a poller. When the time has passed AND the server has already responded, request again.
Since the server response won't change too much right after the response, you can also add a setTimeout
handler in the callback function of your AJAX method.