typescript window.setInterval()工作不正常
typescript window.setInterval() not working properly
我的window.setInterval()方法有问题。下面是一个结构示例,方法"repeat"被重复调用,但我不能调用"repeat"中的任何方法。在这个例子中,当我实例化管理器(let m=new manager())时,它会打印"打印之前",但不会从printStuff方法或"打印之后"消息中打印出日志。
有人知道为什么会发生这种事吗?显然,这不是我的实际代码,因为它足够简单,不在单独的函数中,但是我的实际编码需要调用"repeat"函数中的许多函数,当它发现对另一个函数的调用时,它将停止执行。
class manager{
constructor(){
window.setInterval(this.repeat, 5000);
}
repeat(){
console.log("Before Print");
this.printStuff();
console.log("After Print");
}
printStuff(){
console.log("Print Stuff");
}
Set interval将取this.repeat脱离上下文,您需要使用显式"绑定"方法
setInterval(this.repeat.bind(this), 5000)
或
setInterval(()=>this.repeat(), 5000)
相关文章:
- Javascript.getHours()工作不正常
- removeClass函数没有'工作不正常
- jQuery Ajax GET请求工作不正常
- Cordova ng路线工作不正常
- 更新属性工作不正常Meteor/MongoDB
- 为什么文本对齐:对;工作不正常
- Javascript作用域和Ajax调用;工作不正常
- JavaScript DOM insertBefore'工作不正常
- Angular Js布线工作不正常
- onmouseout函数工作不正常
- 承诺工作不正常
- JQuery.live()工作不正常
- Alloy UI文本区域工作不正常
- ng src工作不正常,但src工作正常
- Canvas.toDataUrl(“image/png”)工作不正常
- JQuery `length`属性工作不正常.为什么?
- Javascript setInterval工作不正常
- 我的Javascript代码没有'工作不正常
- 表单验证工作不正常,在不检查条目的情况下继续
- $localStorage array select with ng选项工作不正常