【问题标题】:Difference between Nativescript timer module and JS setIntervalNativescript定时器模块与JS setInterval的区别
【发布时间】:2016-06-09 00:58:13
【问题描述】:

我正在开发一个使用 setInterval 函数的个人项目。我注意到 Nativescript 具有带有 setInterval 的“计时器”模块,并将其实现到我的项目中。

每次我想在我的应用程序中使用 setInterval() 时是否值得导入 Nativescript 计时器模块,或者我可以只使用 Javascript setinterval() 代替吗?

代码示例(带有打字稿的 angular 2):

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)
}

来源:http://docs.nativescript.org/angular/cookbook/timer

顺便说一句,上述方法是实现计时器模块的正确方法吗?到目前为止,我唯一的经验是带有颜色模块的 Angular 2 - Nativescript 教程,但计时器模块略有不同。

【问题讨论】:

    标签: javascript android angular nativescript


    【解决方案1】:

    计时器模块抽象了本机平台计时系统。如果您查看source here for the Android version,您会注意到使用的本机类/方法。至于命名setIntervalclearTimeout 等,正是 NativeScript 团队选择的这些方法的名称。我假设让 javascript 开发人员更容易记住函数的作用。我会默认使用 timer 模块,而不是 NS 应用程序中的 JS 超时/间隔,但这是我的意见。

    至于导入,您的 TS 代码看起来不错。

    【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2021-12-01
    • 2019-04-14
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多