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

Q990 setTimeout doesn't pause the execution of my code, how can I pause the execution of my code?

You are here: irt.org | FAQ | JavaScript | Timeout | Q990 [ previous next ]

It depends how you write your code:

<script language="JavaScript"><!--
function myFunction1() {
    var now1 = new Date();
    setTimeout('myFunction1()',10000);
    var now2 = new Date();
    alert('difference = ' + (Date.parse(now2) - Date.parse(now1)));
}

myFunction1();
//--></script>

will always trigger the alert message 0 milliseconds later, whereas the following:

<script language="JavaScript"><!--
var now1 = now2 = '';
function myFunction1() {
    now1 = new Date();
    setTimeout('myFunction2()',10000);
}

function myFunction2() {
    now2 = new Date();
    alert('difference = ' + (Date.parse(now2) - Date.parse(now1)));
    myFunction1();
}

myFunction1();
//--></script>

will always trigger the alert 10000 milliseconds later.

©2018 Martin Webb