您好,我希望您可以帮我解决这个问题。如何在3或4秒内停止此代码?
setTimeout(function() {
setInterval(function() {
document.getElementById("my").style.borderColor = "red";
}, 1000);
}, 500);
setInterval(function() {
document.getElementById("my").style.borderColor = "blue";
}, 1000);
}
答案 0 :(得分:0)
使用window.clearInterval
方法:
setTimeout(function () {
clearInterval(blueInterval);
clearInterval(redInterval);
}, timeToBlink);
答案 1 :(得分:0)
var blinks=5;
var myBlink = setInterval(function() { blink(); },1000);
function blink() {
switch(document.getElementById("my").style.borderColor)
{
case("red"):
document.getElementById("my").style.borderColor = "blue";
break;
default:
case("blue"):
document.getElementById("my").style.borderColor = "red";
break;
}
blinks--;
if(blinks<=0)
{
clearInterval(myBlink);
}
}