SYNTAX错误已修复。 Dreamweaver说我的代码在第52行有错误。我看不出有什么问题?也许这与我的if和else语句不起作用有关。
IF AND ELSE STATEMENTS FIXED。如果我单独测试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()中调用它们的,并且尽我所能它是正确的?
答案 0 :(得分:2)
您使用clearInterval()
错误。您正在向它传递一个函数参数,而它需要一个对象。
// correct usage
var time = setInterval(stuff, 100);
clearInterval(time);
答案 1 :(得分:0)
你在别的前面有一个半冒号。 ;
这是语法错误,请将其删除。你也有很多奇怪的地方,我建议你不要把它们放在函数声明之后,如果块阻塞if / else块,循环块等等。
答案 2 :(得分:0)
如果您想在if
语句中测试某些内容是否“相等”,则需要使用==
或===
而不是=
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
希望这有帮助