📺 "One project. One language. Three apps." Learn more in our upcoming code sharing webinar!

NativeScript Core

Timer

The methods provided by timer module allows execution of code fragment at specific time interval. Methods:

  • setTimeout
  • setInterval
const timerModule = require("tns-core-modules/timer");
import { setInterval, clearInterval } from "tns-core-modules/timer";

Interval

Timer method setInterval can be used to apply recurring action on given interval in miliseconds

id = timerModule.setInterval(() => {
    const randNumber = Math.floor(Math.random() * (color.length));
    vm.set("buttoncolor", color[randNumber]);
}, 1000);
id = setInterval(() => {
    const randNumber = Math.floor(Math.random() * (color.length));
    vm.set("buttoncolor", color[randNumber]);
}, 1000);

The Timer modules allows clearing the interval as well via clearInterval method.

timerModule.clearInterval(id);
clearInterval(id);

Improve this document

Demo Source


Timeout

Timer method setTimeout can be used to delay the execution of an action in miliseconds.

setTimeout(() => {
    vm.set("counter", counter--);
    button.backgroundColor = new Color("#30BCFF");
}, 1000);
setTimeout(() => {
    vm.set("counter", counter--);
    button.backgroundColor = new Color("#30BCFF");
}, 1000);

Improve this document

Demo Source


API Reference for the Timer Class