<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: sleep() in JavaScript</title>
	<atom:link href="http://www.phpied.com/sleep-in-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpied.com/sleep-in-javascript/</link>
	<description>Stoyan's blog about &#60;a href="/category/xhtml" class="tag-minor"&#62;(x)html&#60;/a&#62;, &#60;a href="/category/ajax" class="tag-major"&#62;ajax&#60;/a&#62;, &#60;a href="/category/bookmarklets" class="tag-major"&#62;bookmarklets&#60;/a&#62;, &#60;a href="/category/browsers" class="tag-minor"&#62;browsers&#60;/a&#62;, &#60;a href="/category/css" class="tag-normal"&#62;css&#60;/a&#62;, &#60;a href="/category/firebug" class="tag-minor"&#62;firebug&#60;/a&#62;, &#60;a href="/category/javascript" class="tag-numero-uno"&#62;javascript&#60;/a&#62;, &#60;a href="/category/json" class="tag-normal"&#62;json&#60;/a&#62;, &#60;a href="/category/mdb2" class="tag-minor"&#62;mdb2&#60;/a&#62;, &#60;a href="/category/mysql" class="tag-normal"&#62;mysql&#60;/a&#62;, &#60;a href="/category/pear" class="tag-numero-uno"&#62;pear&#60;/a&#62;, &#60;a href="/category/performance" class="tag-major"&#62;performance&#60;/a&#62;, &#60;a href="/category/php" class="tag-numero-uno"&#62;php&#60;/a&#62;, &#60;a href="/category/phpbb" class="tag-major"&#62;phpbb&#60;/a&#62;, &#60;a href="/category/tools" class="tag-normal"&#62;tools&#60;/a&#62;, &#60;a href="/category/yslow" class="tag-minor"&#62;yslow&#60;/a&#62;, &#60;a href="/category/yui" class="tag-normal"&#62;yui&#60;/a&#62;, &#60;a href="/category/writing" class="tag-minor"&#62;writing&#60;/a&#62;, &#60;a href="/category/music" class="tag-major"&#62;music&#60;/a&#62;,... &#60;a href="/category/life-and-everything" class="tag-normal"&#62;life and everything&#60;/a&#62;.</description>
	<pubDate>Thu, 09 Sep 2010 06:26:40 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: droope</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-72698</link>
		<dc:creator>droope</dc:creator>
		<pubDate>Mon, 19 Apr 2010 17:47:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-72698</guid>
		<description>This is my go @ it. I needed something quite different from what you needed.

/*
 * Sleep while condition == true
 * @param int interval - interval between every time sleep checks for condition (MS)
 * @param function/string condition - function that should return wether to continue looping or not. Beware of the scope of variables.
 * @param function/string onFalse - function to be executed when conditions return false. Expects a string like "function()" or a function.
*/
function sleep_while(interval, condition, onFalse) {
	if(eval(condition)) {
		setTimeout("sleep_while("+interval+", "+ condition + ", " + onFalse + ")", interval);
	} else {
		if(typeof( onFalse ) == 'string') {
			eval(onFalse);
		} else {
			onFalse();
		}
	}
}</description>
		<content:encoded><![CDATA[<p>This is my go @ it. I needed something quite different from what you needed.</p>
<p>/*<br />
 * Sleep while condition == true<br />
 * @param int interval - interval between every time sleep checks for condition (MS)<br />
 * @param function/string condition - function that should return wether to continue looping or not. Beware of the scope of variables.<br />
 * @param function/string onFalse - function to be executed when conditions return false. Expects a string like &#8220;function()&#8221; or a function.<br />
*/<br />
function sleep_while(interval, condition, onFalse) {<br />
	if(eval(condition)) {<br />
		setTimeout(&#8221;sleep_while(&#8221;+interval+&#8221;, &#8220;+ condition + &#8220;, &#8221; + onFalse + &#8220;)&#8221;, interval);<br />
	} else {<br />
		if(typeof( onFalse ) == &#8217;string&#8217;) {<br />
			eval(onFalse);<br />
		} else {<br />
			onFalse();<br />
		}<br />
	}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: A</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-71769</link>
		<dc:creator>A</dc:creator>
		<pubDate>Tue, 10 Nov 2009 13:53:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-71769</guid>
		<description>The whole CPU!</description>
		<content:encoded><![CDATA[<p>The whole CPU!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: therealdealsince1982</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-71768</link>
		<dc:creator>therealdealsince1982</dc:creator>
		<pubDate>Tue, 10 Nov 2009 04:10:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-71768</guid>
		<description>I have searched/googled quite a few webpages on javascript sleep... and there is no answer if you want javascript to "RUN, DELAY, RUN"... what most people got was either, "RUN, RUN(useless stuff), RUN" or "RUN, RUN + delayed RUN"....

So I ate some burgers and got thinking:::
here is a solution that works... so you need to chop up your running codes...:::



DISPLAY


//javascript sleep by "therealdealsince1982"; copyrighted 2009
//setInterval
var i = 0;

function run() {
	//pieces of codes to run
	if (i==0){document.getElementById("id1").innerHTML= "code segment "+ i +" is ran"; }
	if (i==1){document.getElementById("id1").innerHTML= "code segment "+ i +" is ran"; }
	if (i==2){document.getElementById("id1").innerHTML= "code segment "+ i +" is ran"; }
	if (i &#62;2){document.getElementById("id1").innerHTML= "code segment "+ i +" is ran"; }
	if (i==5){document.getElementById("id1").innerHTML= "all code segment finished running"; clearInterval(t); } //end interval, stops run
	i++; //segment of code finished running, next...
}

t=setInterval("run()",1000);





//..............................



DISPLAY


//javascript sleep by "therealdealsince1982"; copyrighted 2009
//setTimeout
var i = 0;

function flow() {
	run(i);
	i++; //code segment finished running, increment i; can put elsewhere
	sleep(1000);
	if (i==5) {clearTimeout(t);} //stops flow, must be after sleep()
}

function run(segment) {
	//pieces of codes to run, can use switch statement
	if (segment==0){document.getElementById("id1").innerHTML= "code segment "+ segment +" is ran"; }
	if (segment==1){document.getElementById("id1").innerHTML= "code segment "+ segment +" is ran"; }
	if (segment==2){document.getElementById("id1").innerHTML= "code segment "+ segment +" is ran"; }
	if (segment &#62;2){document.getElementById("id1").innerHTML= "code segment "+ segment +" is ran"; }
}

function sleep(dur) {t=setTimeout("flow()",dur);} //starts flow control again after dur

flow(); //starts flow




//................................................


DISPLAY


//javascript sleep by "therealdealsince1982"; copyrighted 2009
//setTimeout, switch
var i = 0;

function flow() {
	switch(i)
	{
		case 0:
			run(i);
			sleep(1000);
			break;
		case 1:
			run(i);
			sleep(2000);
			break;
		case 5:
			run(i);
			clearTimeout(t); //stops flow
			break;
		default:
			run(i);
			sleep(3000);
			break;
	}
}

function run(segment) {
	//pieces of codes to run, can use switch statement
	if (segment==0){document.getElementById("id1").innerHTML= "code segment "+ segment +" is ran"; }
	if (segment==1){document.getElementById("id1").innerHTML= "code segment "+ segment +" is ran"; }
	if (segment==2){document.getElementById("id1").innerHTML= "code segment "+ segment +" is ran"; }
	if (segment &#62;2){document.getElementById("id1").innerHTML= "code segment "+ segment +" is ran"; }
	i++; //current segment of code finished running, next...
}

function sleep(dur) {t=setTimeout("flow()",dur);} //starts flow control again after dur

flow(); //starts flow control for first time...


</description>
		<content:encoded><![CDATA[<p>I have searched/googled quite a few webpages on javascript sleep&#8230; and there is no answer if you want javascript to &#8220;RUN, DELAY, RUN&#8221;&#8230; what most people got was either, &#8220;RUN, RUN(useless stuff), RUN&#8221; or &#8220;RUN, RUN + delayed RUN&#8221;&#8230;.</p>
<p>So I ate some burgers and got thinking:::<br />
here is a solution that works&#8230; so you need to chop up your running codes&#8230;:::</p>
<p>DISPLAY</p>
<p>//javascript sleep by &#8220;therealdealsince1982&#8243;; copyrighted 2009<br />
//setInterval<br />
var i = 0;</p>
<p>function run() {<br />
	//pieces of codes to run<br />
	if (i==0){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ i +&#8221; is ran&#8221;; }<br />
	if (i==1){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ i +&#8221; is ran&#8221;; }<br />
	if (i==2){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ i +&#8221; is ran&#8221;; }<br />
	if (i &gt;2){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ i +&#8221; is ran&#8221;; }<br />
	if (i==5){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;all code segment finished running&#8221;; clearInterval(t); } //end interval, stops run<br />
	i++; //segment of code finished running, next&#8230;<br />
}</p>
<p>t=setInterval(&#8221;run()&#8221;,1000);</p>
<p>//&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;</p>
<p>DISPLAY</p>
<p>//javascript sleep by &#8220;therealdealsince1982&#8243;; copyrighted 2009<br />
//setTimeout<br />
var i = 0;</p>
<p>function flow() {<br />
	run(i);<br />
	i++; //code segment finished running, increment i; can put elsewhere<br />
	sleep(1000);<br />
	if (i==5) {clearTimeout(t);} //stops flow, must be after sleep()<br />
}</p>
<p>function run(segment) {<br />
	//pieces of codes to run, can use switch statement<br />
	if (segment==0){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment==1){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment==2){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment &gt;2){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
}</p>
<p>function sleep(dur) {t=setTimeout(&#8221;flow()&#8221;,dur);} //starts flow control again after dur</p>
<p>flow(); //starts flow</p>
<p>//&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;</p>
<p>DISPLAY</p>
<p>//javascript sleep by &#8220;therealdealsince1982&#8243;; copyrighted 2009<br />
//setTimeout, switch<br />
var i = 0;</p>
<p>function flow() {<br />
	switch(i)<br />
	{<br />
		case 0:<br />
			run(i);<br />
			sleep(1000);<br />
			break;<br />
		case 1:<br />
			run(i);<br />
			sleep(2000);<br />
			break;<br />
		case 5:<br />
			run(i);<br />
			clearTimeout(t); //stops flow<br />
			break;<br />
		default:<br />
			run(i);<br />
			sleep(3000);<br />
			break;<br />
	}<br />
}</p>
<p>function run(segment) {<br />
	//pieces of codes to run, can use switch statement<br />
	if (segment==0){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment==1){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment==2){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment &gt;2){document.getElementById(&#8221;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	i++; //current segment of code finished running, next&#8230;<br />
}</p>
<p>function sleep(dur) {t=setTimeout(&#8221;flow()&#8221;,dur);} //starts flow control again after dur</p>
<p>flow(); //starts flow control for first time&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamie Beck</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-71575</link>
		<dc:creator>Jamie Beck</dc:creator>
		<pubDate>Thu, 08 Oct 2009 04:01:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-71575</guid>
		<description>This site shows you a few different ways of creating a sleep function in JavaScript. And how each one affects the browser. 

http://www.devcheater.com

In the end the only thing that is cross browser safe is the setTimeout() and setInterval() functions.</description>
		<content:encoded><![CDATA[<p>This site shows you a few different ways of creating a sleep function in JavaScript. And how each one affects the browser. </p>
<p><a href="http://www.devcheater.com" rel="nofollow">http://www.devcheater.com</a></p>
<p>In the end the only thing that is cross browser safe is the setTimeout() and setInterval() functions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nathan</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-71530</link>
		<dc:creator>Nathan</dc:creator>
		<pubDate>Thu, 24 Sep 2009 18:26:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-71530</guid>
		<description>Okay, so here's the deal with a real sleep vs. setTimeout scenario - I have functionality where under a certain condition I need to display a "modal" dialog box. Because of the complexity of what needs to be displayed inside this box, I cannot use "alert()", "confirm()" or "prompt()". However, I need the current JavaScript thread to halt (yes, I understand that JavaScript is single-threaded, although the inclusion of "setTimeout" and "setInterval" would suggest otherwise) until the box has received all the required input, and has been closed (identical to how "alert()", "confirm()" and "prompt()" operate, so for the love of all that is holy, don't "use setTimeout" me - I've explored using that for a week, and it's NOT a viable solution for me).

Frankly, the amount of quirky coding required to facilitate my needs using setTimeout would be arduous, and would require some truly screwed up process flow - all for the sake of avoiding a "useless" instruction such as "sleep"/"wait"/whatever. It's OBVIOUSLY something which is required, otherwise JavaScript wouldn't include "alert()", "confirm()" and "prompt()", functions which are designed to offer exactly this sort of functionality, but which are overly simplified.</description>
		<content:encoded><![CDATA[<p>Okay, so here&#8217;s the deal with a real sleep vs. setTimeout scenario - I have functionality where under a certain condition I need to display a &#8220;modal&#8221; dialog box. Because of the complexity of what needs to be displayed inside this box, I cannot use &#8220;alert()&#8221;, &#8220;confirm()&#8221; or &#8220;prompt()&#8221;. However, I need the current JavaScript thread to halt (yes, I understand that JavaScript is single-threaded, although the inclusion of &#8220;setTimeout&#8221; and &#8220;setInterval&#8221; would suggest otherwise) until the box has received all the required input, and has been closed (identical to how &#8220;alert()&#8221;, &#8220;confirm()&#8221; and &#8220;prompt()&#8221; operate, so for the love of all that is holy, don&#8217;t &#8220;use setTimeout&#8221; me - I&#8217;ve explored using that for a week, and it&#8217;s NOT a viable solution for me).</p>
<p>Frankly, the amount of quirky coding required to facilitate my needs using setTimeout would be arduous, and would require some truly screwed up process flow - all for the sake of avoiding a &#8220;useless&#8221; instruction such as &#8220;sleep&#8221;/&#8221;wait&#8221;/whatever. It&#8217;s OBVIOUSLY something which is required, otherwise JavaScript wouldn&#8217;t include &#8220;alert()&#8221;, &#8220;confirm()&#8221; and &#8220;prompt()&#8221;, functions which are designed to offer exactly this sort of functionality, but which are overly simplified.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stoyan</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-70843</link>
		<dc:creator>Stoyan</dc:creator>
		<pubDate>Thu, 14 May 2009 04:11:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-70843</guid>
		<description>In your case looks like setTimeout is the thing to do. JS is single-threaded, but you have two options. For latest/greatest browsers there are the web workers - https://wiki.mozilla.org/DOMWorkerThreads_current

Otherwise, setTimeout with a 1ms timeout will allow you to unlock the thread and not block the UI. so the recipe is 
1. do a chunk of computation
2. setTimeout to schedule the next part in 1ms
... repeat till work is done</description>
		<content:encoded><![CDATA[<p>In your case looks like setTimeout is the thing to do. JS is single-threaded, but you have two options. For latest/greatest browsers there are the web workers - <a href="https://wiki.mozilla.org/DOMWorkerThreads_current" rel="nofollow">https://wiki.mozilla.org/DOMWorkerThreads_current</a></p>
<p>Otherwise, setTimeout with a 1ms timeout will allow you to unlock the thread and not block the UI. so the recipe is<br />
1. do a chunk of computation<br />
2. setTimeout to schedule the next part in 1ms<br />
&#8230; repeat till work is done</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: KingRadical</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-70839</link>
		<dc:creator>KingRadical</dc:creator>
		<pubDate>Wed, 13 May 2009 17:02:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-70839</guid>
		<description>Unfortunately, this is not a sleep. This is a wait. A "sleep" function should idle the processor for a specified time, not blast it into oblivion with work.

Looking for a proper sleep() for JavaScript is becoming tiresome, and JS programmers are retarded and say things like "JUST USE SETTIMEOUT()" because that's not always the *right* way to do something.

For instance, I have a JavaScript app I'm writing right now that loops through about 1500 XML elements to prepare to add markers to a google map. During this process loop, CPUs are maxed out. I want to add an ACTUAL sleep for about 1-5 milliseconds (as in, a wait state where no processing is going on) to the loop to throttle back the CPU load to avoid killing peoples' browsers. Unfortunately, it does not seem possible.</description>
		<content:encoded><![CDATA[<p>Unfortunately, this is not a sleep. This is a wait. A &#8220;sleep&#8221; function should idle the processor for a specified time, not blast it into oblivion with work.</p>
<p>Looking for a proper sleep() for JavaScript is becoming tiresome, and JS programmers are retarded and say things like &#8220;JUST USE SETTIMEOUT()&#8221; because that&#8217;s not always the *right* way to do something.</p>
<p>For instance, I have a JavaScript app I&#8217;m writing right now that loops through about 1500 XML elements to prepare to add markers to a google map. During this process loop, CPUs are maxed out. I want to add an ACTUAL sleep for about 1-5 milliseconds (as in, a wait state where no processing is going on) to the loop to throttle back the CPU load to avoid killing peoples&#8217; browsers. Unfortunately, it does not seem possible.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-70796</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Thu, 07 May 2009 01:05:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-70796</guid>
		<description>@Marc: "Notice he says he is making a sleep function that behaves like the PHP sleep() function, and it does." Does PHP's sleep() function peg your CPU at 100% while it's sleeping? That's the issue here. This is appropriate for his testing use, but not for anything you'd want to inflict on a regular website user. +1 for marking this "do not use unless you really know what you're doing."</description>
		<content:encoded><![CDATA[<p>@Marc: &#8220;Notice he says he is making a sleep function that behaves like the PHP sleep() function, and it does.&#8221; Does PHP&#8217;s sleep() function peg your CPU at 100% while it&#8217;s sleeping? That&#8217;s the issue here. This is appropriate for his testing use, but not for anything you&#8217;d want to inflict on a regular website user. +1 for marking this &#8220;do not use unless you really know what you&#8217;re doing.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marc</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-70219</link>
		<dc:creator>Marc</dc:creator>
		<pubDate>Thu, 12 Feb 2009 09:11:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-70219</guid>
		<description>Everyone who is saying this function doesnt work like ti should is wrong.

This is 100% what it should do. Notice he says he is making a sleep function that behaves like the PHP sleep() function, and it does.

Thanks for it!</description>
		<content:encoded><![CDATA[<p>Everyone who is saying this function doesnt work like ti should is wrong.</p>
<p>This is 100% what it should do. Notice he says he is making a sleep function that behaves like the PHP sleep() function, and it does.</p>
<p>Thanks for it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tudor</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-70144</link>
		<dc:creator>Tudor</dc:creator>
		<pubDate>Thu, 22 Jan 2009 06:24:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpied.com/sleep-in-javascript/#comment-70144</guid>
		<description>Yes, unfortunately it boosts the CPU's load at over 90% and is not asynchronous. Perhaps you should name the function "wait".</description>
		<content:encoded><![CDATA[<p>Yes, unfortunately it boosts the CPU&#8217;s load at over 90% and is not asynchronous. Perhaps you should name the function &#8220;wait&#8221;.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
