Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q640 How do you check for events whilst in a loop?

You are here: irt.org | FAQ | JavaScript | Event | Q640 [ previous next ]

JavaScript does not stop to check for events whilst in a loop. You have to break the loop up, and replace it with timeouts:

<HTML>

<HEAD>

<SCRIPT LANGUAGE="JavaScript"><!--
var running = true;
var timer;

function loop() {
    while (running) timer = window.setTimeout('loop()',100) ; // loop every 100 milliseconds
}

function stop() {
    alert('stopping');
    window.clearTimeout(timer);
    running = false;
}
//--></SCRIPT>

</HEAD>

<BODY>

<FORM>
<INPUT TYPE="BUTTON" VALUE="Stop" onClick="stop()">
</FORM>

<SCRIPT LANGUAGE="JavaScript"><!--
loop();
//--></SCRIPT>

</HTML>

©2018 Martin Webb