<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: sleep() in JavaScript</title>
	<link>http://www.phpied.com/sleep-in-javascript/</link>
	<description>Stoyan's blog about (x)html, ajax, bookmarklets, browsers, css, firebug, javascript, json, mdb2, mysql, pear, performance, php, phpbb, tools, yslow, yui, writing, music,... life and everything.</description>
	<pubDate>Tue, 13 May 2008 03:10:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.1</generator>

	<item>
		<title>by: Jason Harwig</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-64137</link>
		<pubDate>Wed, 26 Mar 2008 13:01:39 +0000</pubDate>
		<guid>http://www.phpied.com/sleep-in-javascript/#comment-64137</guid>
					<description>Ok, fair enough.  I would just put something that says &quot;Don't actually do this unless you know what your doing&quot;.  Someone who doesn't understand js internals might use your function when they really should use setTimeout.</description>
		<content:encoded><![CDATA[<p>Ok, fair enough.  I would just put something that says "Don't actually do this unless you know what your doing".  Someone who doesn't understand js internals might use your function when they really should use setTimeout.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Stoyan</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-64136</link>
		<pubDate>Wed, 26 Mar 2008 07:52:25 +0000</pubDate>
		<guid>http://www.phpied.com/sleep-in-javascript/#comment-64136</guid>
					<description>Thanks for the link Jason.

The thing I had in mind is that sometimes that's exactly what scripts do, they are so busy that they freeze that browser for a part of a second or two. For performance experiments that's what I wanted to simulate: a very busy script that locks the browser. 

I don't think setTimeout will simulate exactly that. As Stuart shows above, if you use setTimeout() with even 0 milliseconds delay, this tells the browser: &quot;finish what're doing and when you have a chance, execute what I've set with a timeout&quot;. In Stuart's example the two dates are logged (part of the current thread) and the sleep happens right after that.</description>
		<content:encoded><![CDATA[<p>Thanks for the link Jason.</p>
<p>The thing I had in mind is that sometimes that's exactly what scripts do, they are so busy that they freeze that browser for a part of a second or two. For performance experiments that's what I wanted to simulate: a very busy script that locks the browser. </p>
<p>I don't think setTimeout will simulate exactly that. As Stuart shows above, if you use setTimeout() with even 0 milliseconds delay, this tells the browser: "finish what're doing and when you have a chance, execute what I've set with a timeout". In Stuart's example the two dates are logged (part of the current thread) and the sleep happens right after that.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Jason Harwig</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-64134</link>
		<pubDate>Wed, 26 Mar 2008 02:26:17 +0000</pubDate>
		<guid>http://www.phpied.com/sleep-in-javascript/#comment-64134</guid>
					<description>I second what Philip said -- This is a very bad idea.  JavaScript is single threaded so while that for loop is running nothing else can execute (js timers, browser events, even the UI in most browsers).  Try to sleep for 5 or more seconds and the browser will even warn the user that a script is running slowly.

Just use setTimeout.

If you really want a sleep function then try &quot;Narative JavaScript&quot; here: http://www.neilmix.com/narrativejs/doc/index.html.  It's a pre-proccessor that allows you to use blocking code that it then changes to async code for you.</description>
		<content:encoded><![CDATA[<p>I second what Philip said &#8212; This is a very bad idea.  JavaScript is single threaded so while that for loop is running nothing else can execute (js timers, browser events, even the UI in most browsers).  Try to sleep for 5 or more seconds and the browser will even warn the user that a script is running slowly.</p>
<p>Just use setTimeout.</p>
<p>If you really want a sleep function then try "Narative JavaScript" here: <a href='http://www.neilmix.com/narrativejs/doc/index.html' rel='nofollow'>http://www.neilmix.com/narrativejs/doc/index.html</a>.  It's a pre-proccessor that allows you to use blocking code that it then changes to async code for you.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Stoyan</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-64127</link>
		<pubDate>Tue, 25 Mar 2008 08:51:03 +0000</pubDate>
		<guid>http://www.phpied.com/sleep-in-javascript/#comment-64127</guid>
					<description>Thanks Stuart, both very good points.</description>
		<content:encoded><![CDATA[<p>Thanks Stuart, both very good points.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Stuart Colville</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-64126</link>
		<pubDate>Tue, 25 Mar 2008 08:41:00 +0000</pubDate>
		<guid>http://www.phpied.com/sleep-in-javascript/#comment-64126</guid>
					<description>@Stoyan: to me a while loop feels more natural:

&lt;code&gt;function sleep(milliseconds) {
  var start = new Date().getTime();
  while ((new Date().getTime() - start) &amp;#60; milliseconds){
    // Do nothing
  }
}

console.log(new Date());
sleep(5000);
console.log(new Date());

Tue Mar 25 2008 08:36:54 GMT+0000 (BST)
Tue Mar 25 2008 08:36:59 GMT+0000 (BST)&lt;/code&gt;

I'm thinking a non-blocking solution could look like this:

&lt;code&gt;function sleep(milliseconds) {
  setTimeout(function(){
      var start = new Date().getTime();
      while ((new Date().getTime() - start) &amp;#60; milliseconds){
        // Do nothing
      }
  },0);
}

console.log(new Date());
sleep(5000);
console.log(new Date());&lt;/code&gt;

The script returns after the sleep but the second logged date is the same as the first:

&lt;code&gt;Tue Mar 25 2008 08:34:51 GMT+0000 (BST)
Tue Mar 25 2008 08:34:51 GMT+0000 (BST)&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@Stoyan: to me a while loop feels more natural:</p>
<p><code>function sleep(milliseconds) {<br />
  var start = new Date().getTime();<br />
  while ((new Date().getTime() - start) &lt; milliseconds){<br />
    // Do nothing<br />
  }<br />
}</p>
<p>console.log(new Date());<br />
sleep(5000);<br />
console.log(new Date());</p>
<p>Tue Mar 25 2008 08:36:54 GMT+0000 (BST)<br />
Tue Mar 25 2008 08:36:59 GMT+0000 (BST)</code></p>
<p>I'm thinking a non-blocking solution could look like this:</p>
<p><code>function sleep(milliseconds) {<br />
  setTimeout(function(){<br />
      var start = new Date().getTime();<br />
      while ((new Date().getTime() - start) &lt; milliseconds){<br />
        // Do nothing<br />
      }<br />
  },0);<br />
}</p>
<p>console.log(new Date());<br />
sleep(5000);<br />
console.log(new Date());</code></p>
<p>The script returns after the sleep but the second logged date is the same as the first:</p>
<p><code>Tue Mar 25 2008 08:34:51 GMT+0000 (BST)<br />
Tue Mar 25 2008 08:34:51 GMT+0000 (BST)</code>
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Stoyan</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-64125</link>
		<pubDate>Tue, 25 Mar 2008 07:33:27 +0000</pubDate>
		<guid>http://www.phpied.com/sleep-in-javascript/#comment-64125</guid>
					<description>You mean sleep while not actually freezing, but just sitting idle? Interesting...</description>
		<content:encoded><![CDATA[<p>You mean sleep while not actually freezing, but just sitting idle? Interesting&#8230;
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Philip Tellis</title>
		<link>http://www.phpied.com/sleep-in-javascript/#comment-64124</link>
		<pubDate>Tue, 25 Mar 2008 05:59:53 +0000</pubDate>
		<guid>http://www.phpied.com/sleep-in-javascript/#comment-64124</guid>
					<description>Eek!  That will kill the browser and possibly the machine.  Javascript is single threaded, so the browser will block while this executes, and the loop itself will just take up a lot of CPU.  I've heard of some libraries that actually do sleep correctly in an asynchronous manner, but I can't remember the name right now.</description>
		<content:encoded><![CDATA[<p>Eek!  That will kill the browser and possibly the machine.  Javascript is single threaded, so the browser will block while this executes, and the loop itself will just take up a lot of CPU.  I've heard of some libraries that actually do sleep correctly in an asynchronous manner, but I can't remember the name right now.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
