<?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>Designsor &#187; throttle</title>
	<atom:link href="http://www.designsor.com/tag/throttle/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designsor.com</link>
	<description>End Web Developer&#039;s Blog</description>
	<lastBuildDate>Sat, 14 Jan 2012 02:35:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Throttling function calls【译文】</title>
		<link>http://www.designsor.com/2010/07/21/throttling-function-calls/</link>
		<comments>http://www.designsor.com/2010/07/21/throttling-function-calls/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 02:05:41 +0000</pubDate>
		<dc:creator>xiaojue</dc:creator>
				<category><![CDATA[前端开发]]></category>
		<category><![CDATA[apply]]></category>
		<category><![CDATA[throttle]]></category>
		<category><![CDATA[异步ajax延迟请求]]></category>

		<guid isPermaLink="false">http://www.designsor.com/?p=874</guid>
		<description><![CDATA[在twitter上看到一篇比较好的推荐文章，至少我感觉又学到了东西，特别拿出来翻译和分享一下。 原文地址：http://javascript.feederss.com/post/2442.html 如果你做过用户文本框提交确认的功能，比如用到了onkeypress，那么你是否想过要减少你的检测函数的执行次数？举个最简单的例子，比如你要确认一个用户名是否存在，那么你可能并不想每次keypress都执行ajax异步检测，因为大多数的时候输入一个单词需要10分之一秒或者更久，那么你就需要控制你的ajax请求的间隔，而不是每次press之后都立刻去进行ajax请求，至少要在那10份之一秒钟让你的文本框处于休眠状态。 所以就会产生这么一个神奇的类似closure的一种javascript方案，下面是创建这个方法的一个简单的例子： function throttle(fn, delay) { var timer = null; return function () { var context = this, args = arguments; clearTimeout(timer); timer = setTimeout(function () { fn.apply(context, args); }, delay); }; } 如果你是在用jquery来工作的话，如果你要写一个keypress的检测，你可以像下面这样使用： $(&#8216;input.username&#8217;).keypress(throttle(function (event) { // do the Ajax request }, 250)); 这里的关键词是this和那个args，this其实就是你预期的那个input，而args是传入的event对象，通过这个方法你就可以准确的控制你的ajax请求是press一次的之后250毫秒过后再次触发了。 大概就是这么一篇文章，但是我承认以前确实没有注意到过，关于那个throttle函数，我在firebug里检测了下，里面最不明白的其实就是那个this和args。通过断点我检测出来了，虽然他文章最后也告诉了。 恩，最后如果你不用jquery的话，原始的js如何使用这个函数呢？ document.getElementById(&#8216;page&#8217;).onclick=throttle(function(e){ alert(e); alert(&#8217;3000 later&#8217;); },3000) 恩。。。就是这么简单……^_^ [...]]]></description>
			<content:encoded><![CDATA[<p>在twitter上看到一篇比较好的推荐文章，至少我感觉又学到了东西，特别拿出来翻译和分享一下。</p>
<p>原文地址：<a href="http://javascript.feederss.com/post/2442.html" target="_blank">http://javascript.feederss.com/post/2442.html</a></p>
<p>如果你做过用户文本框提交确认的功能，比如用到了onkeypress，那么你是否想过要减少你的检测函数的执行次数？举个最简单的例子，比如你要确认一个用户名是否存在，那么你可能并不想每次keypress都执行ajax异步检测，因为大多数的时候输入一个单词需要10分之一秒或者更久，那么你就需要控制你的ajax请求的间隔，而不是每次press之后都立刻去进行ajax请求，至少要在那10份之一秒钟让你的文本框处于休眠状态。</p>
<p>所以就会产生这么一个神奇的类似<a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">closure</a>的一种javascript方案，下面是创建这个方法的一个简单的例子：</p>
<p><span style="color: #ff0000;">function throttle(fn, delay) {<br />
  var timer = null;<br />
  return function () {<br />
    var context = this, args = arguments;<br />
    clearTimeout(timer);<br />
    timer = setTimeout(function () {<br />
      fn.apply(context, args);<br />
    }, delay);<br />
  };<br />
}</p>
<p></span></p>
<p>如果你是在用jquery来工作的话，如果你要写一个keypress的检测，你可以像下面这样使用：</p>
<p><span style="color: #ff0000;">$(&#8216;input.username&#8217;).keypress(throttle(function (event) {<br />
  // do the Ajax request<br />
}, 250));<br />
</span></p>
<p>这里的关键词是this和那个args，this其实就是你预期的那个input，而args是传入的event对象，通过这个方法你就可以准确的控制你的ajax请求是press一次的之后250毫秒过后再次触发了。</p>
<p>大概就是这么一篇文章，但是我承认以前确实没有注意到过，关于那个throttle函数，我在firebug里检测了下，里面最不明白的其实就是那个this和args。通过断点我检测出来了，虽然他文章最后也告诉了。</p>
<p>恩，最后如果你不用jquery的话，原始的js如何使用这个函数呢？</p>
<p><span style="color: #ff0000;">document.getElementById(&#8216;page&#8217;).onclick=throttle(function(e){<br />
alert(e);<br />
alert(&#8217;3000 later&#8217;);<br />
},3000)</span></p>
<p>恩。。。就是这么简单……^_^</p>
<p>对了 这个文章的作者挺NB的，我也推荐订阅他的tw和rss……</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designsor.com/2010/07/21/throttling-function-calls/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

