Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

jQuery Timeout Management

Friday, February 1st, 2013 by Servage

When you develop something with Javascript an jQuery, you may wish to use timeouts to gain a finer control over events and how they are triggered. Let’s take scrolling as an example. When a user finishes to scroll, you wish to trigger and event. Unfortunately there is no “finish scroll event” in jQuery – since this is hard to determine and up to individual use-cases when exactly you wish the scroll to be registered as ended (the timeout).

Example

You register that the user starts to scroll – so you can fire an event called scrollStop, which is fired as long as the browser detects the user is scrolling. At some point, when the user stops to scroll, the event will stop being fired. Therefore the last time the event has fired, it should actually recognize that “ok, now the user actually stopped to scroll” – and do whatever you want done upon the scroll stop.

The above scenario can be handled with clever use of timeout management, like the following example demonstrates by use of this this jQuery plugin:

$(window).scroll(function(){
   $.doTimeout( 'scroll', 250, function(){
      // do something computationally expensive
   });
});

This is a very smart and easy way to use timeout to register when a continuos event stops.

jQuery Timeout Management, 4.8 out of 5 based on 5 ratings
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.