[英]JavaScript - how to toggle setInterval
in my Vue app I am trying to toggle between two Intervals, depends on the switcher flag (true or false) I want to display "This is first interval" or "This is second interval".在我的 Vue 应用程序中,我试图在两个间隔之间切换,这取决于切换器标志(真或假)我想显示“这是第一个间隔”或“这是第二个间隔”。 My method:
我的方法:
data() {
return {
switcher: false
}
},
methods: {
switchInterval() {
this.switcher = !this.switcher;
if(this.switcher) {
let firstInterval = setInterval(() => {
console.log('This is first interval')
}, 1000)
if(this.switcher === false) clearInterval(firstInterval);
}
if(!this.switcher) {
let secondInterval = setInterval(() =>{
console.log('This is second interval')
}, 1000);
if(this.chatSwitch === true) clearInterval(secondInterval);
}
}
Could anybody help me with this?有人可以帮我解决这个问题吗?
toggleInterval() {
let handler;
this.switcher = !this.switcher;
if (this.switcher) {
handler = () => {
console.log('This is first interval');
};
} else {
handler = () => {
console.log('This is first interval');
};
}
clearInterval(this.intervalId);
this.intervalId = setInterval(handler, 1000);
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.