WindowTimer

Adds methods to implement the HTML5 WindowTimers interface on a given object.

Adds the following methods:

  • clearInterval
  • clearTimeout
  • setInterval
  • setTimeout

See http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html for the complete specification of these methods.

Example Usage Browser/nodeJS compatibility in UnityBase:

// Note:  "this" refers to the global object in this example
    var WindowTimer = require('WindowTimer');
    var timerLoop = WindowTimer.makeWindowTimer(this, function (ms) { sleep(ms); });

    setTimeout(function(){console.log('second function end');}, 2000);
    setTimeout(function(){console.log('first function end');}, 1000);
    console.log('before loop');
    timerLoop();
    console.log('after loop');

For more esoteric uses, timerLoop will return instead of sleeping if passed true which will run only events which are pending at the moment timerLoop is called:

// Note:  "this" refers to the global object in this example
var timerLoop = makeWindowTimer(this, java.lang.Thread.sleep);

// Run code which may add intervals/timeouts

while (timerLoop(true)) {
    print("Still waiting...");
    // Do other work here, possibly adding more intervals/timeouts
}

Methods

makeWindowTimer(target, sleep)function inner

Arguments:
  1. target (Object)  Object to which the methods should be added.
  2. sleep (function)  A function which sleeps for a specified number of milliseconds.