<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:series="http://unfoldingneurons.com/"
		>
<channel>
	<title>Comments on: Sleep or wait function in javascript</title>
	<atom:link href="http://www.digimantra.com/tutorials/sleep-or-wait-function-in-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.digimantra.com/tutorials/sleep-or-wait-function-in-javascript/</link>
	<description>Technology tips for all</description>
	<lastBuildDate>Thu, 09 Sep 2010 21:00:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: therealdealsince1982</title>
		<link>http://www.digimantra.com/tutorials/sleep-or-wait-function-in-javascript/#comment-10302</link>
		<dc:creator>therealdealsince1982</dc:creator>
		<pubDate>Tue, 10 Nov 2009 04:35:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.digimantra.com/?p=707#comment-10302</guid>
		<description>I have searched/googled quite a few webpages on javascript sleep... and there is NO answer if you want javascript to &quot;RUN, DELAY, RUN&quot;... what most people got was either, &quot;RUN, RUN(useless stuff), RUN&quot; or &quot;RUN, RUN + delayed RUN&quot;....

So I ate some burgers and got thinking:::
here is a solution that works... but you have to chop up your running codes...:::

replace &lt;.. with &lt; to run..
//.........................................
//example1:



DISPLAY


//javascript sleep by &quot;therealdealsince1982&quot;; copyrighted 2009
//setInterval
var i = 0;

function run() {
	//pieces of codes to run
	if (i==0){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ i +&quot; is ran&quot;; }
	if (i==1){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ i +&quot; is ran&quot;; }
	if (i==2){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ i +&quot; is ran&quot;; }
	if (i &gt;2){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ i +&quot; is ran&quot;; }
	if (i==5){document.getElementById(&quot;id1&quot;).innerHTML= &quot;all code segment finished running&quot;; clearInterval(t); } //end interval, stops run
	i++; //segment of code finished running, next...
}

t=setInterval(&quot;run()&quot;,1000);





//....................................
//example2:



DISPLAY


//javascript sleep by &quot;therealdealsince1982&quot;; 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(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ segment +&quot; is ran&quot;; }
	if (segment==1){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ segment +&quot; is ran&quot;; }
	if (segment==2){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ segment +&quot; is ran&quot;; }
	if (segment &gt;2){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ segment +&quot; is ran&quot;; }
}

function sleep(dur) {t=setTimeout(&quot;flow()&quot;,dur);} //starts flow control again after dur

flow(); //starts flow




//.....................................
//example3:



DISPLAY


//javascript sleep by &quot;therealdealsince1982&quot;; 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(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ segment +&quot; is ran&quot;; }
	if (segment==1){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ segment +&quot; is ran&quot;; }
	if (segment==2){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ segment +&quot; is ran&quot;; }
	if (segment &gt;2){document.getElementById(&quot;id1&quot;).innerHTML= &quot;code segment &quot;+ segment +&quot; is ran&quot;; }
	i++; //current segment of code finished running, next...
}

function sleep(dur) {t=setTimeout(&quot;flow()&quot;,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; but you have to chop up your running codes&#8230;:::</p>
<p>replace &lt;.. with &lt; to run..<br />
//&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<br />
//example1:</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(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ i +&#8221; is ran&#8221;; }<br />
	if (i==1){document.getElementById(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ i +&#8221; is ran&#8221;; }<br />
	if (i==2){document.getElementById(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ i +&#8221; is ran&#8221;; }<br />
	if (i &gt;2){document.getElementById(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ i +&#8221; is ran&#8221;; }<br />
	if (i==5){document.getElementById(&#8220;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(&#8220;run()&#8221;,1000);</p>
<p>//&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<br />
//example2:</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(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment==1){document.getElementById(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment==2){document.getElementById(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment &gt;2){document.getElementById(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
}</p>
<p>function sleep(dur) {t=setTimeout(&#8220;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;.<br />
//example3:</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(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment==1){document.getElementById(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment==2){document.getElementById(&#8220;id1&#8243;).innerHTML= &#8220;code segment &#8220;+ segment +&#8221; is ran&#8221;; }<br />
	if (segment &gt;2){document.getElementById(&#8220;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(&#8220;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: Sachin Khosla</title>
		<link>http://www.digimantra.com/tutorials/sleep-or-wait-function-in-javascript/#comment-1711</link>
		<dc:creator>Sachin Khosla</dc:creator>
		<pubDate>Tue, 16 Jun 2009 04:34:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.digimantra.com/?p=707#comment-1711</guid>
		<description>Yeah sounds good, but when you interact with the server using Ajax application, u probably do not need settimeout, instead you can use sleep with any of the server side language. That is neat :)</description>
		<content:encoded><![CDATA[<p>Yeah sounds good, but when you interact with the server using Ajax application, u probably do not need settimeout, instead you can use sleep with any of the server side language. That is neat <img src='http://www.digimantra.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amit</title>
		<link>http://www.digimantra.com/tutorials/sleep-or-wait-function-in-javascript/#comment-1710</link>
		<dc:creator>Amit</dc:creator>
		<pubDate>Tue, 16 Jun 2009 04:26:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.digimantra.com/?p=707#comment-1710</guid>
		<description>hi Sachin

the problem with sleep and setTimeout is that in a conditon where javascript calls server side code (like in most of todays world Ajax apps) we cant predict t how much time would a DB or any other  server would take 

window.setTimeout(&quot;A()&quot;, 4000);
B();

the method A() hits DB but if the reterival of data from DB takes more than 4 sec B() would be called and application can behave in  unpredicted manner , so the right approach is to use code like :

A(parametersLIst,B)

and to use setTimeout very less as this method ensures proper Callback and sequence in which js methods are called

Regards
Amit</description>
		<content:encoded><![CDATA[<p>hi Sachin</p>
<p>the problem with sleep and setTimeout is that in a conditon where javascript calls server side code (like in most of todays world Ajax apps) we cant predict t how much time would a DB or any other  server would take </p>
<p>window.setTimeout(&#8220;A()&#8221;, 4000);<br />
B();</p>
<p>the method A() hits DB but if the reterival of data from DB takes more than 4 sec B() would be called and application can behave in  unpredicted manner , so the right approach is to use code like :</p>
<p>A(parametersLIst,B)</p>
<p>and to use setTimeout very less as this method ensures proper Callback and sequence in which js methods are called</p>
<p>Regards<br />
Amit</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sachin Khosla</title>
		<link>http://www.digimantra.com/tutorials/sleep-or-wait-function-in-javascript/#comment-1704</link>
		<dc:creator>Sachin Khosla</dc:creator>
		<pubDate>Tue, 16 Jun 2009 03:29:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.digimantra.com/?p=707#comment-1704</guid>
		<description>Hiya Ian, 

Exactly FFX stops the user interface for that while, but i just meant to show ppl. that this can be done. Of course the setTimeout is better 

Thanks for dropping the comment :)</description>
		<content:encoded><![CDATA[<p>Hiya Ian, </p>
<p>Exactly FFX stops the user interface for that while, but i just meant to show ppl. that this can be done. Of course the setTimeout is better </p>
<p>Thanks for dropping the comment <img src='http://www.digimantra.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian Hobson</title>
		<link>http://www.digimantra.com/tutorials/sleep-or-wait-function-in-javascript/#comment-1702</link>
		<dc:creator>Ian Hobson</dc:creator>
		<pubDate>Mon, 15 Jun 2009 21:55:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.digimantra.com/?p=707#comment-1702</guid>
		<description>Hi Sachin, 

The problem with your approach is that it is a busy-wait. IIRC, some browsers will stall the user interface completely while the wait is executing, while others will be sluggish. 

Better to wrap the second part in a function and call it with 

settimeout(&quot;secondpart();&quot;,4000);  at the end of the first part. 

Regards

Ian</description>
		<content:encoded><![CDATA[<p>Hi Sachin, </p>
<p>The problem with your approach is that it is a busy-wait. IIRC, some browsers will stall the user interface completely while the wait is executing, while others will be sluggish. </p>
<p>Better to wrap the second part in a function and call it with </p>
<p>settimeout(&#8220;secondpart();&#8221;,4000);  at the end of the first part. </p>
<p>Regards</p>
<p>Ian</p>
]]></content:encoded>
	</item>
</channel>
</rss>
