1

I'm working on a personal project that uses the setInterval functions. I noticed Nativescript has the 'timer' module with setInterval and implemented that into my project.

Is it worth importing the Nativescript timer module every time I want to use setInterval() in my app or can I just use the Javascript setinterval() instead?

Example of code ( angular 2 with typescript ):

import { setInterval, clearInterval } from 'timer'

/* skip some code */

private setInterval = setInterval;
private clearInterval = clearInterval;

/* skip some code */

time() {
  this.interval = this.setInterval(() => {
   this.duration = this.clock.formatTime(time)
  }, 1)
}

source : http://docs.nativescript.org/angular/cookbook/timer

On a side note, is the above way the correct way to implement the timer module? My only experience so far is the Angular 2 - Nativescript tutorial with the color module but the timer module is slightly different.

Adam j
  • 391
  • 5
  • 18

1 Answers1

4

The timer module abstracts the native platform timing systems. If you view the source here for the Android version you'll notice the native classes/methods used. As for the naming setInterval, clearTimeout, etc. that is just what the NativeScript team chose to call those methods. I'm assuming to make it simple for javascript developers to remember what the functions do. I would default to the timer module opposed to the JS timeouts/intervals in your NS app, but that's my opinion.

As for importing, your TS code looks good.

Brad Martin
  • 5,637
  • 4
  • 28
  • 44
  • Great, thanks for the reply and link. I'll start looking at the source code more often to get a better understanding. – Adam j Jun 09 '16 at 05:38
  • 2
    Worth noting that there is no separate "JS timeouts/intervals". They defer to the timer module: https://github.com/NativeScript/NativeScript/blob/master/tns-core-modules/globals/globals.ts#L85-L89 (also setTimeout / setInterval isn't actually part of ecmaScript, hence not included by V8 itself, see: http://stackoverflow.com/questions/13616102/how-is-settimeout-implemented-in-node-js) – Michael Jun 09 '16 at 05:40