timers

October 22, 2022

timers

Stability: 2 - Stable

The timers module exposes a global API for calling scheduling functions at some future time. Because the timer functions are global, there is no need to call timers using this API.***

The timer function in CloudControl implements an API similar to the timer provided by the Web browser, except that it uses a different internal implementation that is built on the Android Looper-Handler message loop mechanism. The implementation mechanism is more similar to Node.js.

For example, to send the message "hello" after 5 seconds:

setTimeout(function(){
    toastLog("hello")
}, 5000);

It is important to note that these timers are still single-threaded. If the body of the script has a time-consuming operation or a dead loop, the timer set will not be executed in time, e.g.

setTimeout(function(){
    // the statement here will be executed after 10 seconds instead of 5 seconds
    toastLog("hello")
}, 5000);
//pause for 10 seconds
sleep(10000);

And again.

setTimeout(function(){
    // The statement here will never be executed
    toastLog("hello")
}, 5000);
//dead loop
while(true);

setInterval(callback, delay[, . .args])

  • callback {Function} The function to call when the timer reaches a point.
  • delay {number} The number of milliseconds to wait before calling callback.
  • ... .args {any} Optional argument to be passed when calling the callback.

The callback is scheduled to repeat every delay milliseconds. Returns an id for clearInterval().

When delay is less than 0, delay will be set to 0.

setTimeout(callback, delay[, . .args])

  • callback {Function} The function to be called when the timer reaches the point.
  • delay {number} The number of milliseconds to wait before calling callback.
  • ... .args {any} Optional arguments to be passed when calling the callback.

A single callback scheduled to be executed after the delay milliseconds. Returns an id for clearTimeout().

The callback may not be called exactly at the delay millisecond. CloudControl cannot guarantee exactly when callbacks will be fired, nor can it guarantee their order. Callbacks will be called as close to the specified time as possible.

When delay is less than 0, delay will be set to 0.

setImmediate(callback[, . . args])

  • callback {Function} The function to be called at the end of the current round of the Looper loop.
  • ... .args {any} Optional argument to be passed when calling the callback.

The callback is scheduled to be executed immediately, it is triggered after the I/O event callback. Returns an id for clearImmediate().

When setImmediate() is called multiple times, the callback functions are executed sequentially in the order they were created. Each event loop iteration processes the entire callback queue. If an immediate timer is queued by an executing callback, the timer will not be triggered until the next event loop iteration.

The setImmediate(), setInterval(), and setTimeout() methods return the id of the timer that represents the scheduled timer each time. They can be used to cancel the timer and prevent triggering.

clearInterval(id)

  • id {number} The id returned by a setInterval().

Cancels a recurring timer task created by setInterval().

Example.

// send a hello every 5 seconds
var id = setInterval(function(){
    toastLog("hello");
}, 5000);
//Cancel the loop after 1 minute
setTimeout(function(){
    clearInterval(id);
}, 60 * 1000);

clearTimeout(id)

  • id {number} The id returned by a setTimeout().

Cancel a timed task created by setTimeout().

clearImmediate(id)

  • id {number} The id returned by setImmediate().

Cancels an Immediate object created by setImmediate().

Last update:
Contributors: Bruce