我试图在两个条件之间触发 setTimeInterval。条件:
if(sentence.indexOf('Word') != -1){
//settimeInterval for this case (1st case)
}else{
//settimeInterval for this case(2nd case)
}
OnReady 函数进入第二种情况,因为它返回 -1。一段时间后,我将为返回 0 的句子设置一个值,但不知何故它不会进入第一种情况。 我需要清除TimeInterval吗? 代码:
if(this.dateInterval.indexOf('Word') !== -1){
console.log("Goes to case 1");
setInterval(() => {
console.log(this.dateInterval.indexOf('Word'));
},3000);
}else{
console.log("Goes to case 2");
setInterval(() => {
console.log(this.dateInterval.indexOf('Word'));//logging -1 and 0
},3000);
}
最佳答案
为什么不
setInterval(() => {
if (sentence.indexOf('Word') !== -1) {
// do something
} else {
// do something else
}
}, timeout);
关于javascript - 两个条件之间的 SetInterval 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57508432/