多个setInterval冲突问题,同一个函数段里(例如同一个函数)需要先后运行两个setInterval的时候会出错。两者几乎同时并行,不知道网页是怎么加载的。
多个setInterval解决方案
在intervalA结束的时候,在intervalA里面调用另外一个intervalB。
多个setInterval解决代码:
vart=setInterval(functionA(){
//dosomething…
//addconditiontoendintervalA
if(){
clearInterval(t);
t=null;
B();//callfunctionB
}
},time)
functionB(){
setInterval(functionC(){
//dosth
},time)
}
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""">
<htmlxmlns="">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
<title>测试</title>
</head>
<script>
varfirstInterval;
varsecondInterval;
functionstart(){
firstAlert();
secondAlert();
}
functionfirstAlert(){
if(firstInterval){
clearInterval(firstInterval);
}
fun1();
firstInterval=setInterval('firstAlert()',10000);
}
总之,多个setInterval冲突给大家简单的介绍了一些,希望大家多看看。