•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 329,038 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,495 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 34466 | Replies: 6
![]() |
There is no "sleep" or "wait" operator in JavaScript. You can set a timer, though, and when the timer expires, it will execute a function.
You could have it wait so many milliseconds, and then execute an empty procedure, I suppose.
setTimeout("alert('hello')",1250);You could have it wait so many milliseconds, and then execute an empty procedure, I suppose.
•
•
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation:
Rep Power: 4
Solved Threads: 2
What you usually do is what Tom has suggested, but you would invoke a call to the code that you want to execute after timeout.
So if you had an algorithm like such:
You would implement this with:
This effective 'pauses' the execution of your code.
So if you had an algorithm like such:
function myFunction(){
doSomething();
wait(500);
doSomethingElse();
}You would implement this with:
function myFunction(){
doSomething();
setTimeout(function(){doSomethingElse();}, 500);
}This effective 'pauses' the execution of your code.
•
•
Join Date: Dec 2006
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
This solution is not really a sleep. It effectively postpones the execution of function called by the setTimeout method, but javasctipy is asynchronous, so the execution of the commands following the setTimeout method will happend immediately after.
After a couple of google queries I found the only solution to sleep was to execute this function:
Problem with this approach is when you need a few of shor sleeps one after another. In this case the only thing that happends is the cpu usage is gonna hit the roof and the script gets paused till the last pause is executed.
After a couple of google queries I found the only solution to sleep was to execute this function:
function pause(millis)
{
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis)
} Problem with this approach is when you need a few of shor sleeps one after another. In this case the only thing that happends is the cpu usage is gonna hit the roof and the script gets paused till the last pause is executed.
Last edited by nemo5 : Dec 31st, 2006 at 8:26 am. Reason: code section change
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
The two approaches (do while, and dosomething/settimeout(dosomeelse)) are used in different situations.
1. do while
You just want to pause, but are not expecting anything to happen in the browser while waiting (no document loading, no event). The javascript interpreter will be busy executing the while loop.
(Check http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm)
2. dosomething/settimeout(dosomeelse)
If you want your code to wait for something to happen, like a dom node being created or loaded you have to resort to other tactics.
Joseph Moore at http://40withegg.com/2007/1/5/a-non-...ript-wait-loop calls it a non-cpu intensive wait. Meaning the browser can still function and thus perform job that your code is waiting on.
1. do while
You just want to pause, but are not expecting anything to happen in the browser while waiting (no document loading, no event). The javascript interpreter will be busy executing the while loop.
(Check http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm)
2. dosomething/settimeout(dosomeelse)
If you want your code to wait for something to happen, like a dom node being created or loaded you have to resort to other tactics.
Joseph Moore at http://40withegg.com/2007/1/5/a-non-...ript-wait-loop calls it a non-cpu intensive wait. Meaning the browser can still function and thus perform job that your code is waiting on.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
•
•
•
•
ajax asp beta bon browser browsers browsing developer development echo email encryption europe firefox gecko home html internet internet explorer internet explorer 7 javascript leak linux memory microsoft mozilla msdn networking news office open source open-source patch phishing scams security site social software sql super testing users vista web webmail
- database connection(select ,insert query) within javascript function (JSP)
- Javascript Function to reload DOM Element? (JavaScript / DHTML / AJAX)
- Writing a javascript function (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Problems with browser compatibility.. especially internet explorer
- Next Thread: Error handling



Linear Mode