简体   繁体   English

clearInterval()不起作用

[英]clearInterval() not working

SYNTAX ERRORS FIXED. 语法错误已修复。 Dreamweaver says that I have an error in my code on line 52. I can't see what is wrong? Dreamweaver在第52行上说我的代码有错误。我看不到什么地方出了错? Maybe this has something to do with the fact that my if and else statements are not working. 也许这与我的if和else语句不起作用有关。

IF AND ELSE STATEMENTS FIXED. 如果及其他陈述均已修正。 If I test the if and else statements individually they work correctly, but when I run the entire document, they do not run. 如果我分别测试if和else语句,它们将正常工作,但是当我运行整个文档时,它们将不会运行。 Could someone tell me if the error and my if/else statements not working are related or if there is something else wrong. 有人可以告诉我该错误和我的if / else语句是否起作用或是否存在其他错误。

<script>
var car1=new Object();
car1.Name="Rusty";
car1.Speed="1px", 40;
car1.Distance=0;

var car2=new Object();
car2.Name="Dusty";
car2.Speed="2px", 40; 
car2.Distance=0;

var car3=new Object();
car3.Name="Crankshaft";
car3.Speed="4px", 40;
car3.Distance=0;

function runRusty(){

setInterval(function(){moveRusty()},40);

    function moveRusty(){car1.Distance=car1.Distance +1
        document.getElementById("rusty").style.marginLeft=car1.Distance + "px" 
        };
};


function runDusty(){

setInterval(function(){moveDusty()},40);

    function moveDusty(){car2.Distance=car2.Distance +1
        document.getElementById("dusty").style.marginLeft=car2.Distance + "px" 
        };
 };


function runCrankshaft(){

setInterval(function(){moveCrank()},40);

    function moveCrank(){car3.Distance=car3.Distance +1
          document.getElementById("crankshaft").style.marginLeft=car3.Distance + "px" 
        };
};


function winLose () {if (document.getElementById("rusty").style.marginLeft="900px"){
                     alert("Winner is Rusty!");
                     clearInterval(runRusty);
                     }

else    if(document.getElementById("dusty").style.marginLeft="900px"){
                    alert("Winner is Dusty!");
                    clearInterval(runDusty);
                    }

                else if(document.getElementById("crankshaft").style.marginLeft="900px"){
                    alert("Winner is Crankshaft!");
                    clearInterval(runCrankshaft);
                    };
                };
</script>

EDIT: 编辑:

The if and else statements now work as I changed declared my functions globally and changed them slightly. 当我更改时,if和else语句现在可以正常工作,并在全局范围内声明了我的函数,并对其进行了些微更改。 The code now looks as such: 现在的代码如下所示:

    var car1=new Object();
    car1.Name="Rusty";
    car1.Speed=1;
car1.Distance=0;

var car2=new Object();
car2.Name="Dusty";
car2.Speed=2; 
car2.Distance=0;

var car3=new Object();
car3.Name="Crankshaft";
car3.Speed=4;
car3.Distance=0;

var moveRusty;
var moveDusty;
var moveCrankShaft;


function runRusty(){

moveRusty=setInterval(function(){

        car1.Distance=car1.Distance + car1.Speed;
        document.getElementById("rusty").style.marginLeft=car1.Distance + "px";
        winLose();
        },40);
};


function runDusty(){

moveDusty=setInterval(function(){

        car2.Distance=car2.Distance + car2.Speed;
        document.getElementById("dusty").style.marginLeft=car2.Distance + "px"; 
        winLose();
        },40);
};

function runCrankshaft(){

moveCrankShaft=setInterval(function(){

        car3.Distance=car3.Distance + car3.Speed;
        document.getElementById("crankshaft").style.marginLeft=car3.Distance + "px"; 
        winLose();
        },40);
}

function winLose () {

    if (car1.Distance >= 900){
        alert("Winner is Rusty!");
        car1.Distance = 0;
        car1.Speed = 0;
        car2.Distance = 0;
        car2.Speed = 0;
        car3.Distance = 0;
        car3.Speed = 0;



    }

    else 

    if(car2.Distance >= 900){
        alert("Winner is Dusty!");
        car1.Distance = 0;
        car1.Speed = 0;
        car2.Distance = 0;
        car2.Speed = 0;
        car3.Distance = 0;
        car3.Speed = 0;


    }

    else 

    if(car3.Distance >= 900){
        alert("Winner is Crankshaft!");
        car1.Distance = 0;
        car1.Speed = 0;
        car2.Distance = 0;
        car2.Speed = 0;
        car3.Distance = 0;
        car3.Speed = 0;

    }
};

clearInterval(moveRusty);
clearInterval(moveDusty);
clearInterval(moveCrankShaft);

Everything works now. 现在一切正常。 All the alerts happen and the position of the cars is reset. 发生所有警报,并重置汽车的位置。 But all my clearInterval() do not work. 但是我所有的clearInterval()都不起作用。 I don't know if clearInterval() is the correct thing to use. 我不知道clearInterval()是否使用正确。 What I want to do is reset the page so that I can run the "game" again. 我想做的是重置页面,以便我可以再次运行“游戏”。 Right now, the only way to do that is by refreshing the page. 现在,唯一的方法就是刷新页面。 I've double checked how I've declared my setInterval() and how I'm calling them in my clearInterval(), and as far as I can it's right? 我已经仔细检查了如何声明setInterval()以及如何在clearInterval()中调用它们,据我所知,这是正确的吗?

You are using clearInterval() wrong. 您正在使用clearInterval()错误。 You are passing it a function parameter whereas it expects an object. 您传递给它一个函数参数,而它期望一个对象。

// correct usage
var time = setInterval(stuff, 100);
clearInterval(time);

You have a semi colon before the else. 您在另一个之前有一个半冒号。 ; which is a syntax error, remove it . 这是语法错误,请将其删除。 You have a lot of them in weird places also, I suggest you don't put them after function declarations, if blocks if/else blocks, loop blocks etc. 您在怪异的地方也有很多它们,我建议您不要将它们放在函数声明之后,if块if / else块,循环块等。

When you want to test if to things are 'equal' in a if statement, you need to use == or === and NOT = 如果要在if语句中测试事物是否“相等”, if需要使用=====和NOT =

if (document.getElementById("crankshaft").style.marginLeft = "900px") // WRONG
if (document.getElementById("crankshaft").style.marginLeft == "900px") // RIGHT
if (document.getElementById("crankshaft").style.marginLeft === "900px") // BETTER

Read the answer to the following question to know the difference between == and === 阅读以下问题的答案,以了解=====之间的区别

Difference between == and === in JavaScript JavaScript中==和===之间的区别

Hope this helps 希望这可以帮助

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

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