所以我有这段代码
function timer()
{
setTimeout(function(){alert("Out of time")}, 3000); //Alerts "Out of time" after 3000 milliseconds
}
function resetTime()
{
timer(); //this is not right, i thought it would override the first function but it just adds another timer as well which is not what I want
}
function stopTime()
{
//What could go here to stop the first function from fully executing before it hits 3000 milliseconds and displays the alert message?
}
函数 timer() 在页面加载时启动,但如果我有一个 stopTime() 按钮并单击它,我如何停止执行第一个函数并阻止它达到 3000 毫秒标记并发出警报“没时间了”?
最佳答案
使用范围覆盖所有函数的变量。
var myTimer;
...
myTimer = setTimeout(...);
...
clearTimeout(myTimer);
关于javascript - 如何用另一个函数javascript停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431590/