简体   繁体   English

Javascript ClearTimeout不起作用

[英]Javascript ClearTimeout doesnt work

I'm looking for a solution for my code. 我正在为我的代码寻找解决方案。 My clearTimeout function doesn't work in the stop(); 我的clearTimeout函数在stop()中不起作用; and start(); 和start(); functions, does anybody know how to fix this? 功能,有人知道如何解决此问题吗?

The stop, start and reset button have all a setTimeout function. 停止,开始和重置按钮均具有setTimeout功能。 What I would like to do is that if is clicked at one of the buttons, the other two buttons have cleartimeout. 我想做的是,如果单击其中一个按钮,则其他两个按钮具有cleartimeout。 But for somehow it doesn't working right now. 但是由于某种原因,它现在无法正常工作。

var isTimerStarted;
var timeOutElse;
var timeOut;
var opnieuw;

function start() {
    clocktimer = setInterval("update()", 1);
    x.start();
    isTimerStarted = true;
    timeOutElse = setTimeout(function(){
        document.getElementById("scherm3").style.display = "block";
        document.getElementById("scherm2.2").style.visibility = "hidden";
    }, 8000)
}

function stop() {
    x.stop();
    clearInterval(clocktimer);
    isTimerStarted = false;
    timeOut = setTimeout(function(){
        document.getElementById("scherm4").style.visibility = "visible";
        document.getElementById("scherm2.2").style.visibility = "hidden"; 
    }, 4000)
}

function reset() {
    stop();
    x.reset();
    update();
    opnieuw = setTimeout(function(){
        document.getElementById("scherm3").style.display = "block";
        document.getElementById("scherm2.2").style.visibility = "hidden"; 
    }, 10000);    
    clearTimeout(timeOut);
    clearTimeout(timeOutElse);
    document.getElementById("buttontimer").value = "Start";
} 

setTimeout(start, 5000);

function toggleTimer(event) {
    if (isTimerStarted) {
        stop();
        event.target.value = 'Start';
        clearTimeout(opnieuw);
        clearTimeout(timeOutElse);
    } else {
        start();
        event.target.value = 'Stop';
        clearTimeout(opnieuw);
        clearTimeout(timeOut);
    }
 }

There's nothing wrong with your code, from what I can tell. 据我所知,您的代码没有错。

But I think you might have forgot to remove a line from it. 但是我认为您可能忘记了从其中删除一行。

setTimeout(start, 5000); is creating timeouts after 5 seconds whatever/whenever you click a thing. 无论您何时点击事物,都在5秒后创建超时。

It is what could create timing issues. 这可能会造成计时问题。

Here's a scenario that could happen: 这是可能发生的情况:

  • you click 你点击
  • toggleTimer() -> start() -> create timeout and interval toggleTimer() -> start() ->创建超时和间隔
  • 5s later your setTimeout(start, 5000) executes and reassign your timeout and interval variables 5s之后,您的setTimeout(start, 5000)执行并重新分配您的超时和间隔变量
  • you re-click 您重新点击
  • latest timeout and interval gets cleared, but not the first ones 最近的超时和间隔被清除,但第一个不清除

Just to be safe, you could clear, at the beginning of each of your functions, the timeouts and intervals you're creating in the said function. 为了安全起见,您可以在每个函数的开头清除在该函数中创建的超时和间隔。

This way, you will always clear timeouts and intervals that gets created. 这样,您将始终清除超时和创建的间隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM