我正在一个项目中,我需要显示添加的CSS样式,然后显示2秒钟,然后恢复到正常状态。您可以看到我添加了边框,然后我希望能够在2秒后将其删除。但是我不希望它遍历其余代码!我尝试了设置超时,但是无法正常工作。
function showComputersMoves() {
document.getElementById('computersMove').style.display = "block";
console.log(computersHand[intervalCount]);
if (computersHand[intervalCount] == "Rock") {
console.log("Yep, i work");
document.getElementById("computerRock").style.border = "solid 2px red";
setTimeout(2000);
document.getElementById("computerRock").style.border = "";
}
else if (computersHand[intervalCount] == "Paper") {
console.log("Yep, i work");
document.getElementById("computerPaper").style.border = "solid 2px red";
intervalCount++;
}
else if (computersHand[intervalCount] == "Scissors") {
console.log("Yep, i work");
document.getElementById("computerScissors").style.border = "solid 2px red";
intervalCount++;
}
if(computersHand.length == intervalCount){
clearInterval();
}
}
答案 0 :(得分:2)
对于setTimeout
来说,语法无效。您需要声明超时后要使用的函数。解决第一条if
语句的方法如下:
if (computersHand[intervalCount] == "Rock") {
console.log("Yep, i work");
document.getElementById("computerRock").style.border = "solid 2px red";
setTimeout(function() {
document.getElementById("computerRock").style.border = "";
}, 2000);
}