setTimeout/setInterval 的 Javascript 循环问题
var flag = 0/1 (default=1)
- 我希望每 30 秒检查一次该标志,直到该标志变为 0(通过我无法控制的外部事件)。
- 当标志为 0 时,30 秒检查应结束,直到标志再次为 1
- 只要标志为 0,屏幕上就会有一些文本闪烁,如果标志返回到 1,则闪烁应停止,30 秒检查应继续。
我尝试使用 setTimeout/setInterval 来做到这一点,但我在将它们与循环组合时遇到问题。
这是从另一个站点执行闪烁的一些代码:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试一下:
如果您通过外部函数更改了
flag
值,则需要留出时间间隔来检查其是否更改。例如,您可以将此间隔更改为 5 秒。所以它会更快地检测到变化。另一种方法是不直接更改
flag
,而是通过 setter 函数来更改。setFlag(1)
在此函数中,您可以设置和禁用间隔。Try this:
If you change the
flag
value by external functions, you need to leave the interval for checking if its changed or not. You can change this interval to 5 sec for ex. so it will faster detect change.Other way is to change
flag
not directly but by setter function for ex.setFlag(1)
and inside this function you can set and disable interval.也许是这样的:
Maybe something like this:
您可以使用(非标准)watch(支持在 Firefox 中)或此跨浏览器版本(支持所有最新的浏览器)来监视标志的更改,相反:
传递给
watch
的函数将在每次flag.value
更改时执行,因此无需使用以下命令监视它:超时。 (当然,如果 30 秒的等待是硬性要求,那么您将返回 setTimeout,或者您需要跟踪自标志上次更改以来所经过的时间。)You could use (the non-standard) watch (supported in Firefox) or this cross-browser version (supported in all recent browsers) to monitor changes to your flag, instead:
The function passed to
watch
will be executed every timeflag.value
changes, so there's no need to monitor it with timeouts. (Of course, if the 30 second wait is a hard requirement, then you're back tosetTimeout
or you'd need to track the elapsed time since the flag last changed.)感谢您的回答,我会尝试一下。这是我自己的尝试,似乎工作正常:
Thanks for your answers I will try them. This is my own try that seems to work OK: