<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Debounce your JavaScript functions</title>
	<atom:link href="http://ajaxian.com/archives/debounce-your-javascript-functions/feed" rel="self" type="application/rss+xml" />
	<link>http://ajaxian.com/archives/debounce-your-javascript-functions</link>
	<description>Cleaning up the web with Ajax</description>
	<lastBuildDate>Thu, 17 May 2012 07:43:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: badabim</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272403</link>
		<dc:creator>badabim</dc:creator>
		<pubDate>Sun, 29 Mar 2009 19:19:06 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272403</guid>
		<description>Wrote up a couple of simple debounce and throttle implementations on jQuery:

jQuery.debounce = function(callback, delay) { 
        var timeout; 
        return function() { 
                clearTimeout(timeout); 
                timeout = setTimeout(callback, delay); 
        } 
} 

jQuery.throttle = function(callback, delay) { 
        var prev = new Date().getTime() - delay; 
        return function() { 
                var now = new Date().getTime(); 
                if (now - prev &gt; delay) { 
                        prev = now; 
                        callback.call(); 
                } 
        } 
}</description>
		<content:encoded><![CDATA[<p>Wrote up a couple of simple debounce and throttle implementations on jQuery:</p>
<p>jQuery.debounce = function(callback, delay) {<br />
        var timeout;<br />
        return function() {<br />
                clearTimeout(timeout);<br />
                timeout = setTimeout(callback, delay);<br />
        }<br />
} </p>
<p>jQuery.throttle = function(callback, delay) {<br />
        var prev = new Date().getTime() &#8211; delay;<br />
        return function() {<br />
                var now = new Date().getTime();<br />
                if (now &#8211; prev &gt; delay) {<br />
                        prev = now;<br />
                        callback.call();<br />
                }<br />
        }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: steida</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272398</link>
		<dc:creator>steida</dc:creator>
		<pubDate>Sat, 28 Mar 2009 18:11:03 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272398</guid>
		<description>Unscriptable - thank you for explanation.
Jaysmith - setTimeout is brittle where time accuracy is needed. Also, setTimeout breaks code flow. Nothing crucial, but sometime important.</description>
		<content:encoded><![CDATA[<p>Unscriptable &#8211; thank you for explanation.<br />
Jaysmith &#8211; setTimeout is brittle where time accuracy is needed. Also, setTimeout breaks code flow. Nothing crucial, but sometime important.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jaysmith</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272320</link>
		<dc:creator>jaysmith</dc:creator>
		<pubDate>Wed, 25 Mar 2009 15:15:19 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272320</guid>
		<description>Steida,
why is setTimeout brittle?</description>
		<content:encoded><![CDATA[<p>Steida,<br />
why is setTimeout brittle?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unscriptable</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272281</link>
		<dc:creator>unscriptable</dc:creator>
		<pubDate>Tue, 24 Mar 2009 02:37:39 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272281</guid>
		<description>@steida: I believe what you&#039;ve posted is yet another throttling implementation.  I suggest you read the whole blog post to understand why debouncing != throttling.</description>
		<content:encoded><![CDATA[<p>@steida: I believe what you&#8217;ve posted is yet another throttling implementation.  I suggest you read the whole blog post to understand why debouncing != throttling.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unscriptable</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272280</link>
		<dc:creator>unscriptable</dc:creator>
		<pubDate>Tue, 24 Mar 2009 02:32:17 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272280</guid>
		<description>@nataxia: there is no danger from a long-running function as with some throttling implementations.  

@PieturP: thanks for the heads-up on the ExtJS implementation.  I&#039;ll check it out!

@Mourner ad @RoryH: thanks for the kudos!

@steida: I don&#039;t know why you think that setTimeout is so brittle.  Care to elaborate?  Btw, your implementation only works if the triggering event is guaranteed to occur &lt;i&gt;at least once &lt;b&gt;after&lt;/b&gt;&lt;/i&gt; the delay expires.  The solutions presented by Mourner and I are &quot;guaranteed&quot; to execute once -- unless, of course, you think that setTimeout is too &quot;brittle&quot; to guarantee anything.</description>
		<content:encoded><![CDATA[<p>@nataxia: there is no danger from a long-running function as with some throttling implementations.  </p>
<p>@PieturP: thanks for the heads-up on the ExtJS implementation.  I&#8217;ll check it out!</p>
<p>@Mourner ad @RoryH: thanks for the kudos!</p>
<p>@steida: I don&#8217;t know why you think that setTimeout is so brittle.  Care to elaborate?  Btw, your implementation only works if the triggering event is guaranteed to occur <i>at least once <b>after</b></i> the delay expires.  The solutions presented by Mourner and I are &#8220;guaranteed&#8221; to execute once &#8212; unless, of course, you think that setTimeout is too &#8220;brittle&#8221; to guarantee anything.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: steida</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272279</link>
		<dc:creator>steida</dc:creator>
		<pubDate>Tue, 24 Mar 2009 01:39:43 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272279</guid>
		<description>… also all solutions based on setTimeout are brittle.
PS: code tag in comments doesn&#039;t work</description>
		<content:encoded><![CDATA[<p>… also all solutions based on setTimeout are brittle.<br />
PS: code tag in comments doesn&#8217;t work</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: steida</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272278</link>
		<dc:creator>steida</dc:creator>
		<pubDate>Tue, 24 Mar 2009 01:37:34 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272278</guid>
		<description>... also all solutions based on setTimeout are brittle.

PS: &lt;code&gt; in comments doesn&#039;t work.&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>&#8230; also all solutions based on setTimeout are brittle.</p>
<p>PS: <code> in comments doesn't work.</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: steida</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272277</link>
		<dc:creator>steida</dc:creator>
		<pubDate>Tue, 24 Mar 2009 01:34:50 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272277</guid>
		<description>Aforementioned code is so ugly. I suggest this:
&lt;code&gt;
Function.prototype.moderate = function(delay, bind) {
    var time, fn = this;
    return function() {
        if (!time &#124;&#124; (new Date().getTime() - time) &gt;= delay) {
            time = new Date().getTime();
            return fn.apply(bind &#124;&#124; null, arguments);
        }
    };
};
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Aforementioned code is so ugly. I suggest this:<br />
<code><br />
Function.prototype.moderate = function(delay, bind) {<br />
    var time, fn = this;<br />
    return function() {<br />
        if (!time || (new Date().getTime() - time) &gt;= delay) {<br />
            time = new Date().getTime();<br />
            return fn.apply(bind || null, arguments);<br />
        }<br />
    };<br />
};<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RoryH</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272270</link>
		<dc:creator>RoryH</dc:creator>
		<pubDate>Mon, 23 Mar 2009 17:19:22 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272270</guid>
		<description>I like it. I also like the design in that it extends the function prototype to have it easily accessible. (spot the Prototype fan)

Thanks for sharing!</description>
		<content:encoded><![CDATA[<p>I like it. I also like the design in that it extends the function prototype to have it easily accessible. (spot the Prototype fan)</p>
<p>Thanks for sharing!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mourner</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272269</link>
		<dc:creator>Mourner</dc:creator>
		<pubDate>Mon, 23 Mar 2009 16:24:59 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272269</guid>
		<description>I wrote a similar function a year ago, and I still use it often - very useful. Glad this trick got attention on Ajaxian, well done John!

&lt;code&gt;Function.prototype.limitExecByInterval = function(time) {
	var lock, execOnUnlock, args, fn = this;
	return function() {
		args = arguments;
		if (!lock) {				
			lock = true;
			var scope = this;
			setTimeout(function(){
				lock = false;
				if (execOnUnlock) {
					args.callee.apply(scope, args);
					execOnUnlock = false;
				}
			}, time);
			return fn.apply(this, args);
		} else execOnUnlock = true;
	}
}&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>I wrote a similar function a year ago, and I still use it often &#8211; very useful. Glad this trick got attention on Ajaxian, well done John!</p>
<p><code>Function.prototype.limitExecByInterval = function(time) {<br />
	var lock, execOnUnlock, args, fn = this;<br />
	return function() {<br />
		args = arguments;<br />
		if (!lock) {<br />
			lock = true;<br />
			var scope = this;<br />
			setTimeout(function(){<br />
				lock = false;<br />
				if (execOnUnlock) {<br />
					args.callee.apply(scope, args);<br />
					execOnUnlock = false;<br />
				}<br />
			}, time);<br />
			return fn.apply(this, args);<br />
		} else execOnUnlock = true;<br />
	}<br />
}</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PieturP</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272268</link>
		<dc:creator>PieturP</dc:creator>
		<pubDate>Mon, 23 Mar 2009 15:50:07 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272268</guid>
		<description>In ExtJS, one can use the &#039;buffer&#039; property to handle events within that timespan as one single event. It ensures that events can&#039;t be thrown too fast, and it is easy as it could possibly get to implement.</description>
		<content:encoded><![CDATA[<p>In ExtJS, one can use the &#8216;buffer&#8217; property to handle events within that timespan as one single event. It ensures that events can&#8217;t be thrown too fast, and it is easy as it could possibly get to implement.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nataxia</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272265</link>
		<dc:creator>nataxia</dc:creator>
		<pubDate>Mon, 23 Mar 2009 15:02:51 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272265</guid>
		<description>I would wonder about the case of a function which takes longer to execute than the specified timeout.</description>
		<content:encoded><![CDATA[<p>I would wonder about the case of a function which takes longer to execute than the specified timeout.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unscriptable</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272263</link>
		<dc:creator>unscriptable</dc:creator>
		<pubDate>Mon, 23 Mar 2009 14:59:05 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272263</guid>
		<description>Thanks @mdmadph and @TomMahieu.  You guys nailed it on the head: we&#039;ve all been doing this in one way or another.  I guess I just needed to put a new name on it (since it&#039;s certainly not throttling) and create one function that can be extended to many cases.  

I found I was writing this sort of functionality at least once in every project.  Before I made it a core function, though, it was much harder for a subsequent programmer to figure out what was happening.  Calling myFunc.debounce() is much much clearer, imho, than setTimeout().

@mdmadph: Agreed!  For user-oriented actions, somewhere around 500 msec is a good starting point.  

@TomMahieu: this debounce function could be used as is in your case if the &lt;i&gt;debounced&lt;/i&gt; method checked for minimal characters.  It also wouldn&#039;t be too hard to extend the debounce function to take an additional Boolean function to check additional rules before firing the &lt;i&gt;debounced&lt;/i&gt; method.</description>
		<content:encoded><![CDATA[<p>Thanks @mdmadph and @TomMahieu.  You guys nailed it on the head: we&#8217;ve all been doing this in one way or another.  I guess I just needed to put a new name on it (since it&#8217;s certainly not throttling) and create one function that can be extended to many cases.  </p>
<p>I found I was writing this sort of functionality at least once in every project.  Before I made it a core function, though, it was much harder for a subsequent programmer to figure out what was happening.  Calling myFunc.debounce() is much much clearer, imho, than setTimeout().</p>
<p>@mdmadph: Agreed!  For user-oriented actions, somewhere around 500 msec is a good starting point.  </p>
<p>@TomMahieu: this debounce function could be used as is in your case if the <i>debounced</i> method checked for minimal characters.  It also wouldn&#8217;t be too hard to extend the debounce function to take an additional Boolean function to check additional rules before firing the <i>debounced</i> method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TomMahieu</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272251</link>
		<dc:creator>TomMahieu</dc:creator>
		<pubDate>Mon, 23 Mar 2009 13:46:23 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272251</guid>
		<description>Cool stuff.

We&#039;ve actually written a dojo component that does a similar thing.  We&#039;ve developed a TextBox that we use for simple searches:  1 search field that performs an xhr request as you type (no &quot;Go&quot; button click required, we respond on keyup events).  Much like Apple&#039;s spotlight search box for example, but in a web context.   

The dojo component is configurable in 2 parameters:  the delay, and the amount of characters that must be minimally typed before a request is sent.  Some keypresses are filtered out (such as arrow keys, function keys).

You can find the code &lt;a href=&quot;http://code.google.com/p/ppwcode/source/browse/javascript/dojo-1.1.1-lib/trunk/src/main/webapp/js/org/ppwcode/dojo/dijit/form/PpwSearchTextBox.js&quot; rel=&quot;nofollow&quot;&gt;here [code.google.com]&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>Cool stuff.</p>
<p>We&#8217;ve actually written a dojo component that does a similar thing.  We&#8217;ve developed a TextBox that we use for simple searches:  1 search field that performs an xhr request as you type (no &#8220;Go&#8221; button click required, we respond on keyup events).  Much like Apple&#8217;s spotlight search box for example, but in a web context.   </p>
<p>The dojo component is configurable in 2 parameters:  the delay, and the amount of characters that must be minimally typed before a request is sent.  Some keypresses are filtered out (such as arrow keys, function keys).</p>
<p>You can find the code <a href="http://code.google.com/p/ppwcode/source/browse/javascript/dojo-1.1.1-lib/trunk/src/main/webapp/js/org/ppwcode/dojo/dijit/form/PpwSearchTextBox.js" rel="nofollow">here [code.google.com]</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mdmadph</title>
		<link>http://ajaxian.com/archives/debounce-your-javascript-functions/comment-page-1#comment-272250</link>
		<dc:creator>mdmadph</dc:creator>
		<pubDate>Mon, 23 Mar 2009 13:19:04 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=6399#comment-272250</guid>
		<description>Good writeup -- been doing this for a while, always try to use a delay of about 500ms, I think.  Usually works out best.</description>
		<content:encoded><![CDATA[<p>Good writeup &#8212; been doing this for a while, always try to use a delay of about 500ms, I think.  Usually works out best.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

