Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为setTimeout创建循环_Javascript_Loops_Settimeout - Fatal编程技术网

Javascript 为setTimeout创建循环

Javascript 为setTimeout创建循环,javascript,loops,settimeout,Javascript,Loops,Settimeout,我有这段代码,它可以很好地工作,用于在计时器结束时创建闪烁效果,但是我确信使用循环可以更容易地完成。谁能给我看一下那个循环,因为我想不出来,提前谢谢 function stopTimer() { clearTimeout(timerID); timerID = null; timerObj.value = save; var theBody = document.getElementById("theBody"); setTimeout(function () { theBody.classN

我有这段代码,它可以很好地工作,用于在计时器结束时创建闪烁效果,但是我确信使用循环可以更容易地完成。谁能给我看一下那个循环,因为我想不出来,提前谢谢

function stopTimer() {
clearTimeout(timerID);
timerID = null;
timerObj.value = save;

var theBody = document.getElementById("theBody");
setTimeout(function () { theBody.className = "white"; }, 0);
setTimeout(function () { theBody.className = "red"; }, 250);
setTimeout(function () { theBody.className = "white"; }, 500);
setTimeout(function () { theBody.className = "red"; }, 750);
setTimeout(function () { theBody.className = "white"; }, 1000);
setTimeout(function () { theBody.className = "red"; }, 1250);
setTimeout(function () { theBody.className = "white"; }, 1500);
setTimeout(function () { theBody.className = "red"; }, 1750);
setTimeout(function () { theBody.className = "white"; }, 2000);
setTimeout(function () { theBody.className = "white"; }, 2250);
setTimeout(function () { theBody.className = "red"; }, 2500);
setTimeout(function () { theBody.className = "white"; }, 2750);
setTimeout(function () { theBody.className = "red"; }, 3000);
setTimeout(function () { theBody.className = "white"; }, 3250);
setTimeout(function () { theBody.className = "red"; }, 3500);
setTimeout(function () { theBody.className = "white"; }, 3750);
setTimeout(function () { theBody.className = "red"; }, 4000);
setTimeout(function () { theBody.className = "white"; }, 4250);

var audio = document.createElement("audio");
audio.src = "alarm.mp3";
theBody.appendChild(audio);

audio.setAttribute("id", "audio");
audio.play();
setTimeout(function () {
    audio.pause();
}, 6000);``

}

您可以使用setInterval,它会在每个时间间隔调用有问题的函数,直到取消为止

var myVar = setInterval(function(){ 
  theBody.className = (theBody.className == "white")? "red" : "white";
}, 250);
要停止执行,您只需执行以下操作:

 clearInterval(myVar);

您可以使用setInterval,它会在每个间隔调用有问题的函数,直到取消为止

var myVar = setInterval(function(){ 
  theBody.className = (theBody.className == "white")? "red" : "white";
}, 250);
要停止执行,您只需执行以下操作:

 clearInterval(myVar);

这里有一个快速/肮脏的方法

function white() { theBody.className = "white"; }
function red()   { theBody.className = "red"; }

for (var i=0; i<=4250; i+=500) {
  setTimeout(white, i);
  setTimeout(red, i+250);
}
function white(){theBody.className=“white”;}
函数red(){theBody.className=“red”;}

对于(var i=0;i,这里有一个快速/肮脏的方法

function white() { theBody.className = "white"; }
function red()   { theBody.className = "red"; }

for (var i=0; i<=4250; i+=500) {
  setTimeout(white, i);
  setTimeout(red, i+250);
}
function white(){theBody.className=“white”;}
函数red(){theBody.className=“red”;}

对于(var i=0;i您可以使用
setInterval
按间隔执行操作,也可以使用
clearInterval
清除该间隔(使其停止)


您可以使用
setInterval
按时间间隔执行操作,也可以使用
clearInterval
清除该时间间隔(使其停止)