简体   繁体   English

Clearinterval JS之后的Setinterval重叠

[英]Setinterval Overlap after Clearinterval JS

The story is a balloon that falls down. 故事是一个坠落的气球。 if you manage to click on the balloon('Stop' function) you win. 如果您设法单击气球(“停止”功能),您将获胜。 otherwise, if it down 450px you lose. 否则,如果下降450像素,您将输掉。

The problem is when I press the balloon: its change the src, alert me and clearInterval - Now I thought that it will stop or wait for the next event. 问题是当我按下气球时:它更改了src,提醒我并清除clearInterval-现在我认为它将停止或等待下一个事件。 but, after I click 'ok' on the alert its keep falling... 但是,在警报上单击“确定”后,警报持续下降...

My Code: 我的代码:

function Start() {
    num = Math.floor(Math.random() * 501);
    document.getElementById("balon").style.left = num + 'px';
    var id = setInterval(function() {
        if (posX > 450 && ok) {
            alert("Looser");
            clearInterval(id);
        } else {
            posX += Vpos * dt;
            document.getElementById("balon").style.top = posX + 'px';
        }
    }, dt);
}

function Stop() {
    document.getElementById("balon").src = 'JS Pics/balon2.gif';
    alert("Winner");
    clearInterval(id);


}

In conclusion, I found some way to stop it by adding Bool variable. 总之,我找到了一种通过添加Bool变量来阻止它的方法。 see below: Anyway, I am not sure why this happens(would be happy for explanation) or another way to fix this. 参见下文: 无论如何,我不确定为什么会发生这种情况(很乐于解释)或解决此问题的另一种方法。 Thanx. 感谢名单。

function Start() {
    num = Math.floor(Math.random() * 501);
    document.getElementById("balon").style.left = num + 'px';
    var id = setInterval(function() {
        if (posX > 450 && ok) {
            alert("Looser");
            clearInterval(id);
        } else {
            if (ok) {
                posX += Vpos * dt;
                document.getElementById("balon").style.top = posX + 'px';
            } else {
                clearInterval(id);
            }
        }
    }, dt);
}

function Stop() {
    ok = false;
    document.getElementById("balon").src = 'JS Pics/balon2.gif';
    alert("Winner");
    clearInterval(id);
}

id is in scope of Start function, so it's undefined in scope of Stop function. idStart函数的范围内,因此在Stop函数的范围内未定义。
You can declare it outside both functions, so they could both access it: 您可以在两个函数之外声明它,以便它们都可以访问它:

var id;

function Start() {
    num = Math.floor(Math.random() * 501);
    document.getElementById("balon").style.left = num + 'px';
    id = setInterval(function () {
        if (posX > 450 && ok) {
            alert("Looser");
            clearInterval(id);
        }
        else {
                posX += Vpos * dt;
                document.getElementById("balon").style.top = posX + 'px';
        }
    }, dt);
}

function Stop() {
    document.getElementById("balon").src = 'JS Pics/balon2.gif';
    alert("Winner");
    clearInterval(id);
}

Your fix with ok works, because you don't declare it with keyword var so it's assigned to window object (or you have declared it in outer scope and forgot to append in code snippet). ok修复的工作原理是,因为您没有用关键字var声明它,所以它被分配给了window对象(或者您已经在外部范围中声明了它,却忘了附加在代码片段中)。

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

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