Javascript 如何停止随机超时功能

Javascript 如何停止随机超时功能,javascript,vue.js,Javascript,Vue.js,我有一个值,每90毫秒加0.015,它会无限大,所以现在我希望它在点击一个按钮后随机停止。我写了代码,但似乎不起作用 数据:{ crashValue:1, }, 方法:{ crashFunction:function(){ 此.crashValue+=0.015; 这个。startTimer(); }, startTimer(){ 让间隔=90 如果(此.crashValue>2.15){ 间隔=80 } 如果(此.crashValue>3.15){ 间隔=70 } 如果(此.crashValu

我有一个值,每90毫秒加0.015,它会无限大,所以现在我希望它在点击一个按钮后随机停止。我写了代码,但似乎不起作用

数据:{
crashValue:1,
},
方法:{
crashFunction:function(){
此.crashValue+=0.015;
这个。startTimer();
},
startTimer(){
让间隔=90
如果(此.crashValue>2.15){
间隔=80
}
如果(此.crashValue>3.15){
间隔=70
}
如果(此.crashValue>4){
间隔=60
}
如果(此.crashValue>6.15){
间隔=55
}
如果(此.crashValue>8){
间隔=48
}
如果(此.crashValue>10){
间隔=35
}
如果(此.crashValue>15){
间隔=26
}
如果(此.crashValue>22){
间隔=16
}
this.tesst=setTimeout(this.crashFunction,interval);
},
随机停止(){
设asd=Math.floor(Math.random()*5000)
console.log(asd)
clearTimeout(()=>this.startTimer(),asd)
},
}
兑现
的文档清楚地显示,唯一的参数是由
设置超时提供的ID

如果要停止“循环”,首先需要传递正确的值,在本例中是
clearTimeout(this.tesst)

如果您希望在随机时间后触发
clearTimeout
,则需要另一个
setTimeout
。。。请参见下面可以运行的示例

var计数=0;
var-timerId=null;
函数startTimer(){
console.log(++count);
timerId=setTimeout(startTimer,1000);
}
函数stopTimer(){
var rnd=Math.floor(Math.random()*5000);
日志(“在”+(rnd/1000)+“秒”内停止);
setTimeout(函数(){
清除超时(timerId);
控制台日志(“停止”);
},rnd);
}
startTimer()

clearTimeout
应该传递从
setTimeout
返回的id,在您的例子中是
clearTimeout(this.tesst)@freefaller仍然不工作,还有一些我不知道的事情。。。检查开发人员工具。Good luckclearTimeout()应与setTimeout()id一起传递。在您的情况下,
this.tesst
。检查
clearTimeout(()=>this.tesst,asd)
@Abinthaha是否仍不工作