🤖 Create a chatbot for your NativeScript app! Learn more at our upcoming webinar.

NativeScript Core

Timer

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

  • setTimeout
  • setInterval

Timer Module Import

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

Setting 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


Setting 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