A Framework for Making Repetitive Asynchronous Requests
Quite frequently when building AJAX applications, you will need your client script to retrieve data from the server at regular intervals. There are numerous example scenarios, and you will meet many in this book, and perhaps many more in your real-world projects.
JavaScript offers four functions that can help achieving repetitive (or scheduled) functionality: setTimeout
, setInterval
, clearTimeout
, and clearInterval
, which can be used like this:
// using setTimeout and clearTimeout timerId = window.setTimeout(“function()”, interval_in_milliseconds); window.clearTimeout(timeId); // using setInterval and clearInterval timerId = window.setInterval(“function()”, interval_in_milliseconds); window.clearInterval(timeId);
setTimeout
causes the function to be executed once, after the specified time period. setInterval
executes the function repeatedly, at the mentioned interval, until clearInterval
is used. In most AJAX scenarios we prefer using...