My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
OInterval  
Overview of OInterval
Updated May 13, 2010 by matr...@mac.com

OInterval

Calls 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 );

key A string containing a unique identifier for the interval
function The callback function to call at regular intervals
interval The number of milliseconds to wait after subsequent calls to OInterval
mode (optional) An object describing the behaviour of OInterval

mode options

Constant Description
OIntervalDefault Default mode for OInterval. OInterval will wait the interval and then call function.
OIntervalImmediate Triggers the function straight away instead of waiting for interval milliseconds first.
OIntervalIdle Does not trigger the function until a subsequent call to OInterval

Benefits

Using 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.

Example

The 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).


Sign in to add a comment
Powered by Google Project Hosting