AsyncPolling
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.
Installation
In the browser
With bower:
bower install async-polling
Then include the script:
In NodeJS
With npm:
npm install async-polling
Then require the module:
var AsyncPolling = ;
Usage
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.
API
Create a polling
var polling = ;
pollingFunc(end)
: [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.delay
: [number
(ms)|object
] the delay between two calls ofpollingFunc
. If the type is notnumber
, the.valueOf()
method of the object will be called to retrieve the amount of milliseconds.
Run the polling
polling;
Stop the polling
polling;
Since the polling function is bound to polling
, one can call this.stop()
from within the polling function:
;
Listen to events
polling;
eventName
: The name of the event for which we register (run
,start
,error
,result
,end
,schedule
,stop
).listener
: The function to call when the specified event occurs.