Maarten Meulendijks Posted January 12, 2012 Share Posted January 12, 2012 I have a question about a little part of a script I am making. The poroblem is that Javascript does the script the in the wrong way, and I don't understand why (and how to solve it). The script has to do this: print starttime, wait three seconds, then print endtime. But what it does is this: wait three seconds, then print starttime and endtime. Thats the wrong way around! How can I make Javascript to write the starttime BEFORE thew loop?????? Thx. --- <html><head><title>test</title></head><body><script type="text/javascript" language="javascript">var run = true;var starttime=new Date().getTime();document.writeln(starttime+"<br />");// why doesn't it do above command, until AFTER below commandwhile (run) { nowtime = new Date().getTime(); if ((nowtime-starttime)>3000) { run = false; document.writeln(nowtime); }; }</script></body></html> Link to comment Share on other sites More sharing options...
Ingolme Posted January 12, 2012 Share Posted January 12, 2012 Because you're making the browser freeze with a loop. The browser tries to finish scripts before printing anything to save rendering time. You should use setTimeout or setInterval for timed events. document.writeln(starttime + "<br/>");setTimeout(3000, function() { document.body.innerHTML += (new Date()).getTime();}); I can't use document.write after the page has loaded because it will clear the whole screen before writing so I'm writing with a better method. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.