|
OInterval
Overview of OInterval
OIntervalCalls functions at given intervals. OInterval differs from window.setInterval because OInterval will wait until the last job has finished before scheduling the next one. Usage// create a new interval OInterval( key, function, interval [, mode] ); // trigger another call OInterval( key );
mode options
BenefitsUsing OInterval ensures that jobs that take an unpredictable amount of time (such as accessing external web services etc.) will not overlap. The code can wait for the job to complete before scheduling the next call to function. ExampleThe following example will make a call to a web service to get updates every second. // create a new interval
OInterval("get-updates", function(){
service.getUpdates({ success: updatesResponse });
}, 1000, OIntervalImmediate);
// handle the response of the web service
function updatesResponse() {
// TODO: handle any response
// trigger the next one by just passing the key
OInterval("get-updates");
}The main call to OInterval sets up the interval, with 1000 milliseconds (1 second) delay. Because we specify OIntervalImmediate, there will be no delay in calling the function the first time - so service.getUpdates will be called immediately. When the service completes its work and returns, our updatesResponse function will be called. This function handles the response and then makes another call to OInterval, this time only specifying the key. This tells OInterval to schedule the next call to function, which will be in 1 seconds time (because we initially set our interval to 1000 milliseconds). | ||||||||||||||||