<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IT Blog &#187; sleep</title>
	<atom:link href="http://www.techfromhel.com/tag/sleep/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techfromhel.com</link>
	<description>Information Technology - our Life</description>
	<lastBuildDate>Tue, 26 Jul 2011 13:21:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Sleeping with Javascript</title>
		<link>http://www.techfromhel.com/2009/06/sleeping-with-javascript/</link>
		<comments>http://www.techfromhel.com/2009/06/sleeping-with-javascript/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 14:33:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[sleep]]></category>

		<guid isPermaLink="false">http://www.techfromhel.com/?p=18</guid>
		<description><![CDATA[<p>When I went looking for a built-in “sleep()” function in Javascript, I quickly learned that it does not exist. This is surprising considering that “sleep()” is in all other languages that I have used. To get around this javascript shortcoming, I decided to create my own sleep function. The result is a function that loops [...]]]></description>
			<content:encoded><![CDATA[<p>When I went looking for a built-in “sleep()” function in  Javascript, I quickly learned that it does not exist. This is surprising  considering that “sleep()” is in all other languages that I have used.  To get around this javascript shortcoming, I decided to create my own  sleep function. The result is a function that loops internally until the  number of seconds passed is greater than the argument value, “naptime”.</p>
<p>Initial tests of “sleep()” were successful, however, I soon  discovered that most browsers will interrupt “sleep()” and show a  “Slow/Busy script” dialog message whenever the “naptime” argument is  greater than 10 seconds (7 seconds in IE). Trying to work around this  problem was too much effort, so I left the script as is. If you use this  function, I recommend a “naptime” of less than 10 seconds so your site  visitors won’t encounter this “slow script” dialog message.</p>
<p>Below are some examples of the javascript “sleep()” function. Just  click on a link below to start sleeping for the preset amount of seconds  shown.</p>
<p>Here is the script source:</p>
<blockquote><p>//A javascript sleep function by Helene D.<br />
jQuery.fn.sleep = function (naptime) {<br />
  		var loop = false;<br />
		var maxiterations = 9000000;<br />
  		var now = new Date();<br />
  		var timestarted = now.getTime();<br />
		naptime=naptime+0;	//convert to number<br />
		if (naptime &gt;0 ) {<br />
			loop = true;<br />
			if (naptime &lt; 100) {	//convert to microseconds<br />
				naptime=(naptime*1000);<br />
				naptime=naptime-940;	//estim. script exec time<br />
			}<br />
		}<br />
		var i=0;<br />
  		while(loop) {<br />
			i++;<br />
  			now = new Date();<br />
  			timenow = now.getTime();<br />
  			if(timenow &#8211; timestarted &gt; naptime) {<br />
  				loop = false;<br />
			} else if (i &gt; maxiterations) { //no infinite loops<br />
				loop = false;<br />
			}<br />
  		}<br />
		return $(this);<br />
  }</p></blockquote>
<p>The usage is: <code>sleep(seconds);</code>. You can also chain it in jQuery like this: <code>$(this).sleep(seconds).replace(wakeUp);</code></p>
<p>If you are not a jQuery user, you can convert this function to plain javascript by replacing <code>jQuery.fn.sleep = function(naptime)</code> with <code>function sleep(naptime)</code> and remove the <code>return $(this)</code> line near the bottom of the script.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techfromhel.com/2009/06/sleeping-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

