2013-03-12 67 views
1

語法錯誤固定。 Dreamweaver說我在第52行的代碼中有一個錯誤。我看不出什麼錯誤?也許這與我的if和else聲明不起作用有關。clearInterval()不起作用

如果和其他條款不成立。如果我單獨測試if和else語句,它們可以正常工作,但是當我運行整個文檔時,它們不會運行。有人可以告訴我,如果錯誤和我的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> 

編輯:

if和else語句現在的工作,因爲我改變了全球範圍內宣佈我的職務,並將其變化不大。代碼現在看起來如此:

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); 

現在一切正常。所有警報都會發生,汽車的位置將被重置。但是我所有的clearInterval()都不起作用。我不知道是否clearInterval()是正確的使用。我想要做的是重置頁面,以便我可以再次運行「遊戲」。現在,唯一的方法就是刷新頁面。我已經仔細檢查了我是如何聲明我的setInterval()以及如何在我的clearInterval()中調用它們的,並且我儘可能地是正確的?

回答

2

您正在使用錯誤的clearInterval()。你傳遞一個函數參數,而它期望一個對象。

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

我創建了一個對象:var rustyTimer = setInterval(function(){moveRusty()},40); \t \t功能moveRusty(){car1.Distance = car1.Distance 1 \t \t \t的document.getElementById( 「生鏽」)style.marginLeft = car1.Distance + 「PX」 \t \t \t。};'所以我clearInterval()是這樣的,現在'功能winLose(){如果(的document.getElementById( 「生鏽」)。style.marginLeft == 「900px」){ \t \t \t \t \t \t警報(「得主是生鏽的!「); \t \t \t \t \t \t clearInterval(rustyTimer); \t \t \t \t \t \t}'但現在我的代碼被打破 - 就像它之前我聲明的對象不起作用把一個對象到調用clearInterval。 ()沒有工作 – 2013-03-12 19:15:43

+0

我也對我的評論中缺乏造型表示歉意。我仍然習慣於這個網站。 – 2013-03-12 19:19:25

0

你在別的之前有一個分號。 ;這是一個語法錯誤,將其刪除。你也有很多奇怪的地方,我建議你不要把它們放在函數聲明之後,如果塊if/else塊,循環塊等。

+0

謝謝!修正了語法錯誤。我的if和else語句仍然不起作用試圖找出 – 2013-03-12 19:05:11

0

當你想測試的東西是否'相等'在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 

閱讀回答以下問題,以瞭解=====

Difference between == and === in JavaScript

0123之間的差異

希望這會有幫助

+0

謝謝,我試過這個,但它沒有改變任何東西。 – 2013-03-12 19:12:19

+0

你是怎樣看待HTML的?你有一個元素與我d ='Rusty' – 2013-03-12 19:18:48

+0

是的,我確實有一個id ='Rusty' – 2013-03-12 19:55:07