Angular可以启动按钮无法停止间隔
我使用的是Angular,并且有此代码:
App.component.ts
started: boolean = false;
…
startStop() {
if (!this.started) {
setInterval(()=> { this.test() }, 1000); // not started so start the interval
this.started = true;
} else if (this.started) {
this.started = false; // Its started so stop it');
clearInterval();
}
}
test() {
// do stuff only via interval if started
}
,然后我有一个按钮将其打开和关闭
App.component.html,
<button (click)=“startStop()”>Start / Stop</button>
即使“启动”更改为false,则间隔开启,但没有接触事件。
如何解决此问题?
I am using Angular and I have this code:
App.component.ts
started: boolean = false;
…
startStop() {
if (!this.started) {
setInterval(()=> { this.test() }, 1000); // not started so start the interval
this.started = true;
} else if (this.started) {
this.started = false; // Its started so stop it');
clearInterval();
}
}
test() {
// do stuff only via interval if started
}
Then I have a button that turns it on and off
app.component.html
<button (click)=“startStop()”>Start / Stop</button>
The interval turns on but not off eventhough the variable "started" is changed to false.
How can I fix this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是
clear Interval
的工作方式。如果您有几个间隔怎么办?它会停止哪一个?setInterval
返回您应该将其传递给clear Interval
的IDThis is not how
clearInterval
works. What if you have several intervals ? Which one will it stop ?setInterval
returns an id that you should pass toclearInterval