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>
Maybe something like this:
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:
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.