<?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>岁月如歌 &#187; animation</title>
	<atom:link href="http://lifesinger.org/blog/tag/animation/feed/" rel="self" type="application/rss+xml" />
	<link>http://lifesinger.org/blog</link>
	<description>关注用户体验、前端开发，记录生活点滴、岁月足迹。</description>
	<lastBuildDate>Mon, 06 Sep 2010 15:05:09 +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>对How JavaScript Timers Work的理解</title>
		<link>http://lifesinger.org/blog/2009/02/john-resig-how-javascript-timers-work/</link>
		<comments>http://lifesinger.org/blog/2009/02/john-resig-how-javascript-timers-work/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:07:11 +0000</pubDate>
		<dc:creator>lifesinger</dc:creator>
				<category><![CDATA[开发]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[setInterval]]></category>
		<category><![CDATA[setTimeout]]></category>

		<guid isPermaLink="false">http://lifesinger.org/blog/?p=1184</guid>
		<description><![CDATA[John Resig在How JavaScript Timers Work一文中，对setTimeout和setInterval的分析很精辟： 总结如下： JavaScript engines only have a single thread, forcing asynchronous events to queue waiting for execution. setTimeout and setInterval are fundamentally different in how they execute asynchronous code. If a timer is blocked from immediately executing it will be delayed until the next possible point of execution (which will be [...]]]></description>
			<content:encoded><![CDATA[<p>John Resig在<a href="http://ejohn.org/blog/how-javascript-timers-work/#postcomment">How JavaScript Timers Work</a>一文中，对setTimeout和setInterval的分析很精辟：</p>
<p style="text-align: center"><a href="http://ejohn.org/files/Timers.png"><img title="Timer" src="http://ejohn.org/files/427px-Timers.png" alt="" width="427" height="320" /></a></p>
<p>总结如下：</p>
<ul>
<li>JavaScript engines only have a single thread, forcing asynchronous events to queue waiting for execution.</li>
<li>setTimeout and setInterval are fundamentally different in how they execute asynchronous code.</li>
<li>If a timer is blocked from immediately executing it will be delayed until the next possible point of execution (which will be longer than the desired delay).</li>
<li>Intervals may execute back-to-back with no delay if they take long enough to execute (longer than the specified delay).</li>
</ul>
<p>我的心得：<span id="more-1184"></span></p>
<ol>
<li>无论是setTimeout还是setInterval，触发时，如果当前进程不为空，都得去排队等待执行，这一点上是无差异的。</li>
<li>区别是，setTimeout只需排一次队，setInterval则需要按照预设的间隔时间，每到时间点都去排一下。</li>
<li>setInterval去排队时，如果发现自己还在队列中未执行，则会被drop掉。也就是说，同一个inerval，在队列里只会有一个。</li>
<li>因为队列机制，无论是setTimeout还是setInterval，第一次触发时的时间，只会等于大于预设时间，不可能小于。</li>
<li>对于setInterval来说，如果执行时间大于预设间隔时间，很可能导致连续执行，中间没有时间间隔，这是很糟糕的，很可能会耗费大量cpu.</li>
</ol>
<p>因此，对于动画来说，如果单帧的执行时间大于间隔时间，用setTimeout比用setInterval更保险。John Resig在回复中也表明了这个观点：</p>
<blockquote><p>It really depends on the situation &#8211; and how the timers are actually being used. setInterval will, most likely, get you more &#8216;frames&#8217; in the animation but will certainly tax your processor more. A lot of frameworks end up using setTimeout since it degrades more gracefully on slower computers.</p></blockquote>
<p>一个简单的测试页面：<a href="http://lifesinger.org/lab/2008/timer_test.html">timer_test.html</a>（请在Chrome下运行，注意那些零值或接近零的值，setInterval没有interval了！）</p>
<p>因此，在这种情况下，采用setTimeout更保险：</p>
<pre>
setTimeout(function(){
    /* Some long block of code... */
    setTimeout(arguments.callee, 10);
  }, 10);
</pre>
<p>当然，大部分情况下，单帧执行时间都小于预设的间隔时间，上面分析的差异，是感觉不大出来的。</p>
]]></content:encoded>
			<wfw:commentRss>http://lifesinger.org/blog/2009/02/john-resig-how-javascript-timers-work/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>写代码这点事儿 &#8211; 从卷帘动画的实现说起</title>
		<link>http://lifesinger.org/blog/2008/08/the-little-things-about-write-code/</link>
		<comments>http://lifesinger.org/blog/2008/08/the-little-things-about-write-code/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 16:04:27 +0000</pubDate>
		<dc:creator>lifesinger</dc:creator>
				<category><![CDATA[开发]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[unobtrusive]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://lifesinger.org/blog/?p=66</guid>
		<description><![CDATA[这是淘宝懒懒交流会第二期的内容，大纲为： 布衣暖、菜根香，平淡滋味长 &#8211; 卷帘动画实例浅析 山重水复，柳暗花明 &#8211; 换个角度看世界 雄关漫道，从头越，长空西风烈 &#8211; 为了明天的偷懒 水至清则无鱼 &#8211; 完美的诠释 此中有真意 &#8211; 思想的魅力 下载：talk_on_jscode.zip (1.8M) 注意：压缩包中的杂感.txt是我在准备这个ppt时随手记录的一些想法，有些没有在ppt里体现，感兴趣的可以都看看，讨论讨论。 PS：PPT提到的直觉最终版中，所有相关的js代码请到Unicorn项目中查看： SVN Link 欢迎大家讨论交流。]]></description>
			<content:encoded><![CDATA[<p>这是淘宝懒懒交流会第二期的内容，大纲为：</p>
<ol>
<li>布衣暖、菜根香，平淡滋味长 &#8211; 卷帘动画实例浅析</li>
<li>山重水复，柳暗花明 &#8211; 换个角度看世界</li>
<li>雄关漫道，从头越，长空西风烈 &#8211; 为了明天的偷懒</li>
<li>水至清则无鱼 &#8211; 完美的诠释</li>
<li>此中有真意 &#8211; 思想的魅力</li>
</ol>
<div class="download">下载：<a href="http://lifesinger.org/blog/wp-content/uploads/2008/08/talk_on_jscode.zip">talk_on_jscode.zip</a> (1.8M)</div>
<p>注意：压缩包中的<strong>杂感.txt</strong>是我在准备这个ppt时随手记录的一些想法，有些没有在ppt里体现，感兴趣的可以都看看，讨论讨论。</p>
<p>PS：PPT提到的直觉最终版中，所有相关的js代码请到Unicorn项目中查看： <a href="http://iunicorn.googlecode.com/svn/trunk/">SVN Link</a></p>
<p>欢迎大家讨论交流。</p>
]]></content:encoded>
			<wfw:commentRss>http://lifesinger.org/blog/2008/08/the-little-things-about-write-code/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
