Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Timeout | Q990 [ previous next ]

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

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.


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.