<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Downshift Your Code</title>
	<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/</link>
	<description>News and Artilces about Designing and Developing with Yahoo! Libraries.</description>
	<pubDate>Sat, 11 Oct 2008 21:19:35 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
		<item>
		<title>By: JS Execution Speed &#171; cs44, aimless and unsystematic.</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-303753</link>
		<dc:creator>JS Execution Speed &#171; cs44, aimless and unsystematic.</dc:creator>
		<pubDate>Tue, 12 Feb 2008 18:07:13 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-303753</guid>
		<description>[...] happening to prevent the UI thread from clobbering itself.Either way, this article saved the day:YUI: Downshift Your CodeWhile we didn&#8217;t follow this article verbatim, it was very helpful when staring this IE [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] happening to prevent the UI thread from clobbering itself.Either way, this article saved the day:YUI: Downshift Your CodeWhile we didn&#8217;t follow this article verbatim, it was very helpful when staring this IE [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Muyser</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-268576</link>
		<dc:creator>Eric Muyser</dc:creator>
		<pubDate>Sat, 29 Dec 2007 16:46:27 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-268576</guid>
		<description>Nice tip Nicholas =)

The examples provided by Jeremy Vinding would allow the function to occur the very moment the resize event occurs and every 250ms afterwards as resizing events are sent in Internet Explorer. In contrast, Nicholas' tip mimics Firefox and the function will be called 250ms after resizing has not occurred for 250ms, which we assume means resizing has stopped.

Very nice.</description>
		<content:encoded><![CDATA[<p>Nice tip Nicholas =)</p>
<p>The examples provided by Jeremy Vinding would allow the function to occur the very moment the resize event occurs and every 250ms afterwards as resizing events are sent in Internet Explorer. In contrast, Nicholas&#8217; tip mimics Firefox and the function will be called 250ms after resizing has not occurred for 250ms, which we assume means resizing has stopped.</p>
<p>Very nice.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad Kieffer</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-156442</link>
		<dc:creator>Chad Kieffer</dc:creator>
		<pubDate>Thu, 12 Jul 2007 18:51:25 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-156442</guid>
		<description>Great article and very timely. I'll be using this pattern to throttle resize triggered UI redraws in IE. Thanks!</description>
		<content:encoded><![CDATA[<p>Great article and very timely. I&#8217;ll be using this pattern to throttle resize triggered UI redraws in IE. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doekman</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-156185</link>
		<dc:creator>Doekman</dc:creator>
		<pubDate>Thu, 12 Jul 2007 06:58:21 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-156185</guid>
		<description>@Nicholas: I think, I was sent into the wrong direction by you resize-example...</description>
		<content:encoded><![CDATA[<p>@Nicholas: I think, I was sent into the wrong direction by you resize-example&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dustin Diaz</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155900</link>
		<dc:creator>Dustin Diaz</dc:creator>
		<pubDate>Wed, 11 Jul 2007 20:22:03 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155900</guid>
		<description>That's a good tip Nicholas. This advise also applies to listening to events like onTween or onDrag for the animation and drag 'n drop utilities where it might be best (sometimes) to simply just poll at a slower interval and fire off those events.</description>
		<content:encoded><![CDATA[<p>That&#8217;s a good tip Nicholas. This advise also applies to listening to events like onTween or onDrag for the animation and drag &#8216;n drop utilities where it might be best (sometimes) to simply just poll at a slower interval and fire off those events.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicholas C. Zakas</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155842</link>
		<dc:creator>Nicholas C. Zakas</dc:creator>
		<pubDate>Wed, 11 Jul 2007 17:19:08 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155842</guid>
		<description>@Doekman - You are absolutely correct. If you call process() repeatedly, nothing will happen...until 250ms &lt;em&gt;after&lt;/em&gt; the last call, which is what this pattern is all about.</description>
		<content:encoded><![CDATA[<p>@Doekman - You are absolutely correct. If you call process() repeatedly, nothing will happen&#8230;until 250ms <em>after</em> the last call, which is what this pattern is all about.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Caridy Patiño</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155812</link>
		<dc:creator>Caridy Patiño</dc:creator>
		<pubDate>Wed, 11 Jul 2007 15:40:10 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155812</guid>
		<description>@Nicholas - my intention with the pattern was to show a possible solution for an scenery, and I was trying to delineate the responsibilities of each methods, in the first play I was thinking in this scenery:

A push button to start an animation (250ms animation), you can click the button during the animation, but this event will not affect the animation process. 

The responsibility of the process methods are:
1. To offer a public entry to start the process
2. To determine if the entry is available
3. To control the availability timer

The responsibility of the _process method are:
1. This method is a behavior who get the event, and drive it in to the certain component or apply certain functionalities directly.
2. This method could be outside of the object (YAHOO.example.MyObject) and using another scope.

Here is a new scenery:
If the process method also has a new responsibility: collecting the event information (ex: e.type), and eventually you need another information from the event (ex: mouse position). In this case you need to modify the process method, to solve the problem, and from the point of view of the maintenance and the logic of the business, the _process method is the one who must do this job. 

I'm seeing this in 3 layers:
1. process method: mechanism to control the availability of the real method
2. _process method: collect the event information, and begin a certain behavior
3. external or internal method: who do the real job

@Doekman - About your code:

1. In the first pass, you call _process, and set the timeout
2. (less than 250ms) you set the _work2do=true only
3. when timeout arrive, (the event object was expired, you can mitigate this issue passing the event's attributes according to Nicholas), you call _process and set the work2do=false, and set the time out again
4. when the second time out arrive, you call _process again, with the last event reference (who already expire, twice)
5. And at this point, you're back to the first state...

Ok, I got it, you vary my pattern to warrant the execution of the last event (even when this event are fired between a delay's process).</description>
		<content:encoded><![CDATA[<p>@Nicholas - my intention with the pattern was to show a possible solution for an scenery, and I was trying to delineate the responsibilities of each methods, in the first play I was thinking in this scenery:</p>
<p>A push button to start an animation (250ms animation), you can click the button during the animation, but this event will not affect the animation process. </p>
<p>The responsibility of the process methods are:<br />
1. To offer a public entry to start the process<br />
2. To determine if the entry is available<br />
3. To control the availability timer</p>
<p>The responsibility of the _process method are:<br />
1. This method is a behavior who get the event, and drive it in to the certain component or apply certain functionalities directly.<br />
2. This method could be outside of the object (YAHOO.example.MyObject) and using another scope.</p>
<p>Here is a new scenery:<br />
If the process method also has a new responsibility: collecting the event information (ex: e.type), and eventually you need another information from the event (ex: mouse position). In this case you need to modify the process method, to solve the problem, and from the point of view of the maintenance and the logic of the business, the _process method is the one who must do this job. </p>
<p>I&#8217;m seeing this in 3 layers:<br />
1. process method: mechanism to control the availability of the real method<br />
2. _process method: collect the event information, and begin a certain behavior<br />
3. external or internal method: who do the real job</p>
<p>@Doekman - About your code:</p>
<p>1. In the first pass, you call _process, and set the timeout<br />
2. (less than 250ms) you set the _work2do=true only<br />
3. when timeout arrive, (the event object was expired, you can mitigate this issue passing the event&#8217;s attributes according to Nicholas), you call _process and set the work2do=false, and set the time out again<br />
4. when the second time out arrive, you call _process again, with the last event reference (who already expire, twice)<br />
5. And at this point, you&#8217;re back to the first state&#8230;</p>
<p>Ok, I got it, you vary my pattern to warrant the execution of the last event (even when this event are fired between a delay&#8217;s process).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doekman</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155722</link>
		<dc:creator>Doekman</dc:creator>
		<pubDate>Wed, 11 Jul 2007 09:50:27 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155722</guid>
		<description>@Nicholas: I was actually trying to say politely: when you constantly call "process", nothing happens.

@Caridy: your solution is much better, but I consider it the same pattern, only a better revision. The only thing is you might not handle the last call to "process". 

My call:

&lt;code&gt;
YAHOO.example.MyObject = {

  _timeoutId : 0,
  _work2do : false,
  _process : function ( e ) {
    //processing code goes here
  },

  process : function ( e ) {
    var me = this;
    var handler = function() {
      me._process( e );
      if (me._work2do) {
        me._work2do = false;
        me._timeoutId = setTimeout(handler, 250);
      }
    }
    this._work2do=true;
    if (!this._timeoutId) {
      handler();
    }
  }
};
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@Nicholas: I was actually trying to say politely: when you constantly call &#8220;process&#8221;, nothing happens.</p>
<p>@Caridy: your solution is much better, but I consider it the same pattern, only a better revision. The only thing is you might not handle the last call to &#8220;process&#8221;. </p>
<p>My call:</p>
<p><code><br />
YAHOO.example.MyObject = {</p>
<p>  _timeoutId : 0,<br />
  _work2do : false,<br />
  _process : function ( e ) {<br />
    //processing code goes here<br />
  },</p>
<p>  process : function ( e ) {<br />
    var me = this;<br />
    var handler = function() {<br />
      me._process( e );<br />
      if (me._work2do) {<br />
        me._work2do = false;<br />
        me._timeoutId = setTimeout(handler, 250);<br />
      }<br />
    }<br />
    this._work2do=true;<br />
    if (!this._timeoutId) {<br />
      handler();<br />
    }<br />
  }<br />
};<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicholas C. Zakas</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155344</link>
		<dc:creator>Nicholas C. Zakas</dc:creator>
		<pubDate>Tue, 10 Jul 2007 18:32:51 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155344</guid>
		<description>@Doekman - this technique can be used to mimic FF's resize behavior, however, that's not it's only purpose. Imagine a behavior that's attached to a button. If the user clicks it too often, it may spawn too many calls to the same function, slowing the browser down. This technique can effectively be used to throttle processing at any point in your application

I wouldn't recommend using setInterval() because it will cause the processing to occur at periodic intervals; this code makes the case that you don't want it to happen at regular intervals, however, you want to make sure it doesn't happen too often within a set period of time.

@Caridy - That is a good question. It's my position that processing should be separate from event handlers and the event object should never be passed as an argument to function that does processing (please see &lt;a href="http://yuiblog.com/blog/2007/05/25/video-zakas/" title="Maintainable JavaScript" rel="nofollow"&gt;my talk&lt;/a&gt; for more details on that).

I'd recommend assigning an event handler that, in turn, calls process(), such as:

&lt;code&gt;function handleResize(e){
    YAHOO.example.MyObject.process();
}&lt;/code&gt;

If you need information on the event object, I'd recommend just passing in the information that you need rather than the entire event object (again, for maintainability purposes):

&lt;code&gt;function handleResize(e){
    YAHOO.example.MyObject.process(e.type);
}&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@Doekman - this technique can be used to mimic FF&#8217;s resize behavior, however, that&#8217;s not it&#8217;s only purpose. Imagine a behavior that&#8217;s attached to a button. If the user clicks it too often, it may spawn too many calls to the same function, slowing the browser down. This technique can effectively be used to throttle processing at any point in your application</p>
<p>I wouldn&#8217;t recommend using setInterval() because it will cause the processing to occur at periodic intervals; this code makes the case that you don&#8217;t want it to happen at regular intervals, however, you want to make sure it doesn&#8217;t happen too often within a set period of time.</p>
<p>@Caridy - That is a good question. It&#8217;s my position that processing should be separate from event handlers and the event object should never be passed as an argument to function that does processing (please see <a href="http://yuiblog.com/blog/2007/05/25/video-zakas/" title="Maintainable JavaScript" rel="nofollow">my talk</a> for more details on that).</p>
<p>I&#8217;d recommend assigning an event handler that, in turn, calls process(), such as:</p>
<p><code>function handleResize(e){<br />
    YAHOO.example.MyObject.process();<br />
}</code></p>
<p>If you need information on the event object, I&#8217;d recommend just passing in the information that you need rather than the entire event object (again, for maintainability purposes):</p>
<p><code>function handleResize(e){<br />
    YAHOO.example.MyObject.process(e.type);<br />
}</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Caridy Patiño</title>
		<link>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155338</link>
		<dc:creator>Caridy Patiño</dc:creator>
		<pubDate>Tue, 10 Jul 2007 18:18:42 +0000</pubDate>
		<guid>http://yuiblog.com/blog/2007/07/09/downshift-your-code/#comment-155338</guid>
		<description>Hi Nicholas,

As always you came out with the Good stuff, Thanks. In this case I have a couple of notes:

- What if I need the event's reference? in this case the reference can't be passed to the _process method because the event will expire during the delay.

- In your code, the _process method will be called 250ms after the firing, and this is Ok for the example (resizing the window), but if you need to process the event immediately and then the process's method will be unavailable by 250ms, I think this is a useful pattern too.

The alternate pattern is as follows:

&lt;code&gt;
YAHOO.namespace("example");

YAHOO.example.MyObject = {

    _timeoutId : 0,
    _available : true,
    _process : function ( e ) {
        //processing code goes here
    },

    process : function ( e ) {
        var me = this;
        if (this._available) {
		    this._process( e );
		    this._available = false;
		    clearTimeout(this._timeoutId);
	        this._timeoutId = setTimeout(function(){
	            me._available = true;
	        }, 250);
        }
    }
};
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Hi Nicholas,</p>
<p>As always you came out with the Good stuff, Thanks. In this case I have a couple of notes:</p>
<p>- What if I need the event&#8217;s reference? in this case the reference can&#8217;t be passed to the _process method because the event will expire during the delay.</p>
<p>- In your code, the _process method will be called 250ms after the firing, and this is Ok for the example (resizing the window), but if you need to process the event immediately and then the process&#8217;s method will be unavailable by 250ms, I think this is a useful pattern too.</p>
<p>The alternate pattern is as follows:</p>
<p><code><br />
YAHOO.namespace("example");</p>
<p>YAHOO.example.MyObject = {</p>
<p>    _timeoutId : 0,<br />
    _available : true,<br />
    _process : function ( e ) {<br />
        //processing code goes here<br />
    },</p>
<p>    process : function ( e ) {<br />
        var me = this;<br />
        if (this._available) {<br />
		    this._process( e );<br />
		    this._available = false;<br />
		    clearTimeout(this._timeoutId);<br />
	        this._timeoutId = setTimeout(function(){<br />
	            me._available = true;<br />
	        }, 250);<br />
        }<br />
    }<br />
};<br />
</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.329 seconds -->
