使用setTimeout / setInterval的Javascript循环问题 [英] Javascript loop-problem with setTimeout/setInterval
本文介绍了使用setTimeout / setInterval的Javascript循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var flag = 0/1(默认= 1)
- 我希望这个标志每隔30秒反复检查一次,直到标志变为0(由我无法控制的外部事件)。
- 当标志时是0,30秒检查应该结束,直到标志再次为1
- 只要标志为0,一些文本应该在屏幕上闪烁,如果标志返回1则闪烁应该停止并继续30秒检查。
我尝试使用setTimeout / setInterval执行此操作,但我在将它们与循环组合时遇到问题。 / p>
以下是从另一个网站眨眼的一些代码:
< ; HTML>
< head>
< script type =text / javascript>
函数init(){
window.setInterval(blink,1000);
}
函数blink(){
var elm = document.getElementById('blinkDiv');
if(elm.style.color ==#ff0000)
elm.style.color =#fffffff;
else
elm.style.color =#ff0000;
}
< / script>
< / head>
< body onload =init(); style =background-color:#ffffff;>
< div id =blinkDivstyle =color:#ff0000;> some text< / div>
< / body>
< / html>
解决方案
感谢您的回答我会尝试。这是我自己的尝试似乎工作正常:
< script type =text / javascript>
函数controller(){
if(!f)
setTimeout(blink();,1000);
else if(f)
setTimeout(controller();,30000);
}
函数blink(){
var elm = document.getElementById('alert');
if(elm.style.color ==#ff0000)
elm.style.color =#fffffff;
else
elm.style.color =#ff0000;
controller();
}
< / script>
var flag = 0/1 (default=1)
- I want this flag to checked every 30sec over and over until the flag becomes 0 (by an external event that I cant control).
- When the flag is 0 the 30sec-check should end until the flag is 1 again
- As long as the flag is 0 some text should blink on the screen, if the flag goes back to 1 the blinking should stop and the 30sec check should continue.
I tried to do this with setTimeout/setInterval but Im having problem combining them with loops.
Here is some code doing the blinking from another site:
<html>
<head>
<script type="text/javascript">
function init() {
window.setInterval(blink,1000);
}
function blink() {
var elm = document.getElementById('blinkDiv');
if (elm.style.color == "#ff0000")
elm.style.color = "#ffffff";
else
elm.style.color = "#ff0000";
}
</script>
</head>
<body onload="init();" style="background-color: #ffffff;">
<div id="blinkDiv" style="color: #ff0000;">some text</div>
</body>
</html>
解决方案
Thanks for your answers I will try them. This is my own try that seems to work OK:
<script type="text/javascript">
function controller(){
if(!f)
setTimeout("blink();", 1000);
else if(f)
setTimeout("controller();", 30000);
}
function blink(){
var elm = document.getElementById('alert');
if (elm.style.color == "#ff0000")
elm.style.color = "#ffffff";
else
elm.style.color = "#ff0000";
controller();
}
</script>
这篇关于使用setTimeout / setInterval的Javascript循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文