語法錯誤固定。 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()中調用它們的,並且我儘可能地是正確的?
我創建了一個對象: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
我也對我的評論中缺乏造型表示歉意。我仍然習慣於這個網站。 – 2013-03-12 19:19:25