我正在处理一个使用 setInterval 函数的个人项目。我注意到 Nativescript 具有带有 setInterval 的“计时器”模块,并将其实现到我的项目中。
每当我想在我的应用程序中使用 setInterval() 时,是否值得导入 Nativescript 计时器模块,还是我可以只使用 Javascript setinterval() 代替?
代码示例(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)
}
来源:http://docs.nativescript.org/angular/cookbook/timer
附带说明一下,上面的方法是实现定时器模块的正确方法吗?到目前为止,我唯一的经验是使用颜色模块的 Angular 2 - Nativescript 教程,但计时器模块略有不同。
最佳答案
计时器模块抽象了 native 平台计时系统。如果您查看 source here for the Android version您会注意到使用的 native 类/方法。至于setInterval
、clearTimeout
等命名方式,NativeScript团队就是这样命名的。我假设让 javascript 开发人员更容易记住函数的作用。我会默认使用 timer
模块而不是 NS 应用程序中的 JS 超时/间隔,但这是我的意见。
至于导入,您的 TS 代码看起来不错。
关于javascript - Nativescript定时器模块与JS setInterval的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37715286/