本文介绍了如何在功能上停止间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了功能,即间隔.我需要在完成时停止它.这是代码:
I got function, which is interval. And I need to stop it when it finishes. here is the code:
function cut() {
setInterval(function() {
timer()
}, 1000);
}
function timer() {
seconds = seconds - 1;
document.getElementById("timer1").innerHTML = minutes + ":" + seconds;
if (seconds < 10) {
document.getElementById("timer1").innerHTML = minutes + ":" + "0" + seconds
}
if (seconds <= 0) {
clearInterval("cut()");
//another code to be executed
}
}
我可以完全将此clearInterval用于函数吗?还是有另一种解决方案呢?
can I use this clearInterval for function at all? or there is another solution for doing that?
谢谢:)
推荐答案
您必须保留对 setInterval()
返回的句柄的引用,然后将其传递给 clearInterval()
像这样:
You have to keep a reference to the handle returned from setInterval()
and then pass it to clearInterval()
like so:
var cutHandle;
function cut() {
cutHandle = setInterval(timer, 1000);
}
function timer() {
seconds = seconds - 1;
var timer = document.getElementById("timer1");
timer.innerHTML = minutes + ":" + seconds;
if (seconds < 10) {
timer.innerHTML = minutes + ":" + "0" + seconds
}
if (seconds <= 0) {
clearInterval(cutHandle);
//another code to be executed
}
}
这篇关于如何在功能上停止间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!