<?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>Brian Grinstead &#187; event binding</title>
	<atom:link href="http://www.briangrinstead.com/blog/tag/event-binding/feed" rel="self" type="application/rss+xml" />
	<link>http://www.briangrinstead.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 11 Jan 2012 20:23:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>bindWithDelay jQuery Plugin &#8211; Now With Throttling</title>
		<link>http://www.briangrinstead.com/blog/bindwithdelay-throttling</link>
		<comments>http://www.briangrinstead.com/blog/bindwithdelay-throttling#comments</comments>
		<pubDate>Sat, 14 May 2011 00:27:46 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[bindwithdelay]]></category>
		<category><![CDATA[event binding]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/blog/?p=522</guid>
		<description><![CDATA[I decided to add in throttling functionality to the jquery bindWithDelay plugin. This is a nice feature that didn&#8217;t require very many changes to the code (see the: diff). What is throttling? Imagine you are scrolling down a long page for 3 seconds and you have bindWithDelay running with a timeout of 1000ms. By default, [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to add in throttling functionality to the <a href='http://briangrinstead.com/files/bindWithDelay/'>jquery bindWithDelay</a> plugin.  This is a nice feature that didn&#8217;t require very many changes to the code (see the: <a href='https://github.com/bgrins/bindWithDelay/commit/e23c2502458c516e24dd0b16edcde8b2f1636f3f#diff-0'>diff</a>).</p>
<h3>What is throttling?</h3>
<p>Imagine you are scrolling down a long page for 3 seconds and you have bindWithDelay running with a timeout of 1000ms. By default, your event will only fire a second after you have completely finished scrolling. If you have the optional throttling on, it will fire once per second during the scrolling &#8211; a total of 3 times instead of 1.</p>
<p>Checkout the <a href='http://briangrinstead.com/files/bindWithDelay#example'>throttling demo</a> to see what it is doing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/bindwithdelay-throttling/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>bindWithDelay jQuery Plugin</title>
		<link>http://www.briangrinstead.com/blog/bindwithdelay-jquery-plugin</link>
		<comments>http://www.briangrinstead.com/blog/bindwithdelay-jquery-plugin#comments</comments>
		<pubDate>Tue, 10 Aug 2010 18:26:31 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[bindwithdelay]]></category>
		<category><![CDATA[event binding]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/blog/?p=380</guid>
		<description><![CDATA[Sometimes, I want to have a JavaScript event that doesn&#8217;t fire until the native event stops firing for a short timeout. I&#8217;ve needed to use that pattern in almost every project I have worked on. For example, you want to use JavaScript to resize an iframe to 100% height when the window resizes. The resize() [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, I want to have a JavaScript event that doesn&#8217;t fire until the native event stops firing for a short timeout.  I&#8217;ve needed to use that pattern in almost every project I have worked on.</p>
<p><strong>For example</strong>, you want to use JavaScript to resize an iframe to 100% height when the window resizes.  The resize() event can fire dozens of times, and calculating and setting the new height can slow down your page.  I used to implement it like this (notice the extra global variable and indentation with the anonymous function):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> timeout<span style="color: #339933;color: #000000;">;</span>
<span style="color: #003366; font-weight: bold;color: #0000FF;">function</span> doResize<span style="color: #009900;color: #000000;">&#40;</span>e<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
   clearTimeout<span style="color: #009900;color: #000000;">&#40;</span>timeout<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
   timeout <span style="color: #339933;color: #000000;">=</span> setTimeout<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
      <span style="color: #006600; font-style: italic;color: #008000;">// run some code</span>
   <span style="color: #009900;color: #000000;">&#125;</span><span style="color: #339933;color: #000000;">,</span> <span style="color: #CC0000;color: #808080;">200</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
<span style="color: #009900;color: #000000;">&#125;</span>
$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
   $<span style="color: #009900;color: #000000;">&#40;</span>window<span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;resize&quot;</span><span style="color: #339933;color: #000000;">,</span>doResize<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span></pre></div></div>

<p><strong>I wrote a plugin to make this pattern easier, it is called &#8220;bindWithDelay&#8221;.</strong>  The <a href='http://github.com/bgrins/bindWithDelay/'>source code</a> is online, as is a mini <a href='http://briangrinstead.com/files/bindWithDelay/'>project page</a> with a demo.</p>
<p>
This is what the same code looks like with the plugin:
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span> doResize<span style="color: #009900;color: #000000;">&#40;</span>e<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
      <span style="color: #006600; font-style: italic;color: #008000;">// run some code</span>
<span style="color: #009900;color: #000000;">&#125;</span>
$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
   $<span style="color: #009900;color: #000000;">&#40;</span>window<span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">bindWithDelay</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;resize&quot;</span><span style="color: #339933;color: #000000;">,</span> doResize<span style="color: #339933;color: #000000;">,</span> <span style="color: #CC0000;color: #808080;">200</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/bindwithdelay-jquery-plugin/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keep Original Variable State Between Event Binding and Execution</title>
		<link>http://www.briangrinstead.com/blog/keep-original-variable-state-between-event-binding-and-execution</link>
		<comments>http://www.briangrinstead.com/blog/keep-original-variable-state-between-event-binding-and-execution#comments</comments>
		<pubDate>Wed, 15 Jul 2009 13:48:09 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[event binding]]></category>
		<category><![CDATA[event handling]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[variable scope]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/blog/?p=291</guid>
		<description><![CDATA[Or: Binding Events Inside of a Loop with jQuery I wrote an article on the Foliotek Development Blog about saving the state of a variable inside a closure that is not executed immediately. For example, functions passed into event binding or setTimeout(). Here is a quick rundown of the problem and the solution (using the [...]]]></description>
			<content:encoded><![CDATA[<h3>Or: Binding Events Inside of a Loop with jQuery</h3>
<p>I <a href='http://www.foliotek.com/devblog/keep-variable-state-between-event-binding-and-execution/'>wrote an article on the Foliotek Development Blog</a> about saving the state of a variable inside a closure that is not executed immediately. For example, functions passed into event binding or setTimeout(). Here is a quick rundown of the problem and the solution (using the jQuery library). </p>
<h3>The Problem</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
	$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;body&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;&lt;ul id='container'&gt;&lt;/ul&gt;&quot;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;color: #0000FF;">for</span> <span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> i <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span> i <span style="color: #339933;color: #000000;">&lt;</span> <span style="color: #CC0000;color: #808080;">5</span><span style="color: #339933;color: #000000;">;</span> i<span style="color: #339933;color: #000000;">++</span><span style="color: #009900;color: #000000;">&#41;</span>
	<span style="color: #009900;color: #000000;">&#123;</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> $item <span style="color: #339933;color: #000000;">=</span> $<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;&lt;li /&gt;&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;Item &quot;</span> <span style="color: #339933;color: #000000;">+</span> i<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;#container&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;color: #000000;">&#40;</span>$item<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
		$item.<span style="color: #660066;">click</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
			<span style="color: #000066;color: #0000FF;">alert</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;You clicked number &quot;</span> <span style="color: #339933;color: #000000;">+</span> i<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>  <span style="color: #006600; font-style: italic;color: #008000;">// always &quot;You clicked number 5&quot;</span>
		<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
	<span style="color: #009900;color: #000000;">&#125;</span>
<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span></pre></div></div>

<h3>The Solution</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
	$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;body&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;&lt;ul id='container'&gt;&lt;/ul&gt;&quot;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;color: #0000FF;">for</span> <span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> i <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span> i <span style="color: #339933;color: #000000;">&lt;</span> <span style="color: #CC0000;color: #808080;">5</span><span style="color: #339933;color: #000000;">;</span> i<span style="color: #339933;color: #000000;">++</span><span style="color: #009900;color: #000000;">&#41;</span>
	<span style="color: #009900;color: #000000;">&#123;</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> $item <span style="color: #339933;color: #000000;">=</span> $<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;&lt;li /&gt;&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;Item &quot;</span> <span style="color: #339933;color: #000000;">+</span> i<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		$<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;#container&quot;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;color: #000000;">&#40;</span>$item<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
		<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span> <span style="color: #006600; font-style: italic;color: #008000;">// Closure here here instead of &quot;bindItem()&quot;</span>
			<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> ind <span style="color: #339933;color: #000000;">=</span> i<span style="color: #339933;color: #000000;">;</span>
			$item.<span style="color: #660066;">click</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
				<span style="color: #000066;color: #0000FF;">alert</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">&quot;You clicked number &quot;</span> <span style="color: #339933;color: #000000;">+</span> ind<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span> <span style="color: #006600; font-style: italic;color: #008000;">// Works as expected</span>
			<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span> <span style="color: #006600; font-style: italic;color: #008000;">// Execute immediately</span>
	<span style="color: #009900;color: #000000;">&#125;</span>
<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span></pre></div></div>

<p>The solution uses an immediately executing function to create a new scope and declare a variable &#8220;ind&#8221; that is reserved a new space in memory instead of simply referencing &#8220;i&#8221;. Check out the <a href='http://www.foliotek.com/devblog/keep-variable-state-between-event-binding-and-execution/'>full article</a> for more details.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/keep-original-variable-state-between-event-binding-and-execution/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

