<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx</link><description>JavaScript has two ways of delaying execution of code: setInterval and setTimeout . Both take a function or a string as the first parameter, and a number of milliseconds as the second parameter. The only difference is that the code provided to setInterval</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7263009</link><pubDate>Sun, 22 Nov 2009 15:30:34 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7263009</guid><dc:creator>LVS</dc:creator><author>LVS</author><description>&lt;p&gt;@rajbk&lt;/p&gt;
&lt;p&gt;I do not believe setTimeout &amp;quot;waits&amp;quot; the CPU during the timeout period. It instead schedules it with a timer running in the background. This might cause it to take slightly more than the expected timeout period to start executing.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7263009" width="1" height="1"&gt;</description></item><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7186431</link><pubDate>Mon, 31 Aug 2009 12:59:13 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7186431</guid><dc:creator>Brett</dc:creator><author>Brett</author><description>&lt;p&gt;Hey, wanna say thanks - this helped me modify my code and it&amp;#39;s workin great. I have an ajax-based page which shows and hides different div&amp;#39;s, and one is a chat div. So I setup the function that polls the database for new chat messages to run once, and from inside that function it does a check to see if the chat div is visible, and if so, runs a setTimeout to rerun the check message function in 5 seconds. &lt;/p&gt;
&lt;p&gt;If the user moves away from the chat and on to something else (therefore hiding the chat div) the setTimeout doesn&amp;#39;t trigger and the script stops polling the database for new chat messages until you click on the &amp;quot;chat&amp;quot; button again to reactivate it. &lt;/p&gt;
&lt;p&gt;Works perfectly! I was using a setInterval but much happier now. &amp;nbsp;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7186431" width="1" height="1"&gt;</description></item><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7092919</link><pubDate>Tue, 19 May 2009 17:32:06 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7092919</guid><dc:creator>Bertrand Le Roy</dc:creator><author>Bertrand Le Roy</author><description>&lt;p&gt;@Nicholas: that is true, although a setInterval occurrence won't interrupt JS code that may still be running, so there are cases where the real interval will be larger than the one specified. Still, for operations that are sensitive to precise timing, you can take two approaches. First, you can use setInterval and deal with the disadvantages, knowing that at least you get timing that is as precise as it can get in JS. Second, you can use setTimeout and move the renewal of the timeout to the top of the function. This way, the timing should be very close to the one you'd get from setInterval. I should probably mention that I didn't verify this yet. Might be an interesting follow-up post...&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7092919" width="1" height="1"&gt;</description></item><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7092916</link><pubDate>Tue, 19 May 2009 17:22:49 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7092916</guid><dc:creator>Nicholas C. Zakas</dc:creator><author>Nicholas C. Zakas</author><description>&lt;p&gt;Nice writeup. I think the one piece of information that&amp;#39;s missing is the slight difference in the way that setInterval works versus chained setTimeout calls. When using setInterval, the next batch of code will be run in a time that is less than or equal to the indicated interval whereas chained setTimeout calls will run in a time that is greater than or equal to the indicated interval. setInterval can, in a sense, &amp;quot;catch-up&amp;quot; when it&amp;#39;s behind while chained setTimeout calls tends to lose time over multiple executions. This is why setInterval tends to be the preferred way for running complex animations on a page (see &lt;a rel="nofollow" target="_new" href="http://www.schillmania.com/content/projects/javascript-animation-3/"&gt;www.schillmania.com/.../javascript-animation-3&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Thanks again for sharing, I enjoyed this post.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7092916" width="1" height="1"&gt;</description></item><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7092267</link><pubDate>Mon, 18 May 2009 20:28:14 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7092267</guid><dc:creator>Jason</dc:creator><author>Jason</author><description>&lt;p&gt;If you have any complexity in your code, setInterval makes it that much more complex. You have to deal with unexpected behavior when one thread overwrites another&amp;#39;s data. I did this in my early days and learned the hard way to use setTimeout and call it again just as the Evil blog guy says.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7092267" width="1" height="1"&gt;</description></item><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7091798</link><pubDate>Mon, 18 May 2009 05:13:39 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7091798</guid><dc:creator>Bertrand Le Roy</dc:creator><author>Bertrand Le Roy</author><description>&lt;p&gt;@MisterFantastic: if you look at the code a little closer, you'll see that at the end of the callback code, setTimeout is called again for the next iteration.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7091798" width="1" height="1"&gt;</description></item><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7091677</link><pubDate>Mon, 18 May 2009 01:41:30 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7091677</guid><dc:creator>MisterFantastic</dc:creator><author>MisterFantastic</author><description>&lt;p&gt;Nice post. But you said SetInterval will be called on regualr basis whereas settimeout will be called only once. How can i use this in such a situation ?&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Thani&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7091677" width="1" height="1"&gt;</description></item><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7088023</link><pubDate>Fri, 15 May 2009 15:36:30 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7088023</guid><dc:creator>rajbk</dc:creator><author>rajbk</author><description>&lt;p&gt;One thing worth noting is that setTimeout waits for the function to finish executing, waits 5000ms and then call the function again. &lt;/p&gt;
&lt;p&gt;setInterval, on the other hand, will try to execute the function every 5000ms regardless of when the function was last called or how long the function takes to execute.&lt;/p&gt;
&lt;p&gt;Raj&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7088023" width="1" height="1"&gt;</description></item><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7087994</link><pubDate>Fri, 15 May 2009 14:58:02 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7087994</guid><dc:creator>Greg</dc:creator><author>Greg</author><description>&lt;p&gt;It&amp;#39;s not a good idea to rely on timed events in scripting languages inside of a browser.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7087994" width="1" height="1"&gt;</description></item><item><title>re: setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#7087894</link><pubDate>Fri, 15 May 2009 13:17:14 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7087894</guid><dc:creator>joelvarty</dc:creator><author>joelvarty</author><description>&lt;p&gt;Great heads-up on the currying capabilities of javascript. &amp;nbsp;Here is a link from the wiki article with an example implementation: &lt;a rel="nofollow" target="_new" href="http://flesler.blogspot.com/2008/11/haskell-functions-for-javascript.html"&gt;flesler.blogspot.com/.../haskell-functions-for-javascript.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If only Javascript had real threading capabilities, our lives would be a little easier, until then, I think setTimeout is good - but it only makes things slightly less difficult than setInterval, in my opinion. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;For animations and fades and stuff, I quite like having a callback framework such as jQuery provides so that you don&amp;#39;t have to maintain an animation loop.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7087894" width="1" height="1"&gt;</description></item></channel></rss>