An easy way to run reliable polling without messing with setTimeout.
Here is an article explaining why using
setInterval is discouraged, especially when dealing with asynchronous tasks.
In the browser
bower install async-polling
Then include the script:
npm install async-polling
Then require the module:
var AsyncPolling = ;
Here is the basic usage:
You can also send a result to the
end callback with the usual signature
(error, result). Pass
null as first argument when everythin is fine:
var polling =;polling;polling;polling; // Let's start polling.
See also the demo script.
Create a polling
var polling = ;
function] The function to run periodically; takes a callback as parameter to notify the end of the process and possibly send a result. It will be bound to the polling object.
object] the delay between two calls of
pollingFunc. If the type is not
.valueOf()method of the object will be called to retrieve the amount of milliseconds.
Run the polling
Stop the polling
Since the polling function is bound to
polling, one can call
this.stop() from within the polling function:
Listen to events
eventName: The name of the event for which we register (
listener: The function to call when the specified event occurs.