]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - resources/jquery/jquery.delayedBind.js
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / resources / jquery / jquery.delayedBind.js
1 (function( $ ) {
2 /**
3  * Function that escapes spaces in event names. This is needed because
4  * "_delayedBind-foo bar-1000" refers to two events
5  */
6 function encodeEvent( event ) {
7         return event.replace( /-/g, '--' ).replace( / /g, '-' );
8 }
9
10 $.fn.extend( {
11         /**
12          * Bind a callback to an event in a delayed fashion.
13          * In detail, this means that the callback will be called a certain
14          * time after the event fires, but the timer is reset every time
15          * the event fires.
16          * @param timeout Number of milliseconds to wait
17          * @param event Name of the event (string)
18          * @param data Data to pass to the event handler (optional)
19          * @param callback Function to call
20          */
21         delayedBind: function( timeout, event, data, callback ) {
22                 var encEvent = encodeEvent( event );
23                 return this.each( function() {
24                         var that = this;
25                         // Bind the top half
26                         // Do this only once for every (event, timeout) pair
27                         if (  !( $(this).data( '_delayedBindBound-' + encEvent + '-' + timeout ) ) ) {
28                                 $(this).data( '_delayedBindBound-' + encEvent + '-' + timeout, true );
29                                 $(this).bind( event, function() {
30                                         var timerID = $(this).data( '_delayedBindTimerID-' + encEvent + '-' + timeout );
31                                         // Cancel the running timer
32                                         if ( typeof timerID != 'undefined' )
33                                                 clearTimeout( timerID );
34                                         timerID = setTimeout( function() {
35                                                 $(that).trigger( '_delayedBind-' + encEvent + '-' + timeout );
36                                         }, timeout );
37                                         $(this).data( '_delayedBindTimerID-' + encEvent + '-' + timeout, timerID );
38                                 } );
39                         }
40                         
41                         // Bottom half
42                         $(this).bind( '_delayedBind-' + encEvent + '-' + timeout, data, callback );
43                 } );
44         },
45         
46         /**
47          * Cancel the timers for delayed events on the selected elements.
48          */
49         delayedBindCancel: function( timeout, event ) {
50                 var encEvent = encodeEvent( event );
51                 return this.each( function() {
52                         var timerID = $(this).data( '_delayedBindTimerID-' + encEvent + '-' + timeout );
53                         if ( typeof timerID != 'undefined' )
54                                 clearTimeout( timerID );
55                 } );
56         },
57         
58         /**
59          * Unbind an event bound with delayedBind()
60          */
61         delayedBindUnbind: function( timeout, event, callback ) {
62                 var encEvent = encodeEvent( event );
63                 return this.each( function() {
64                         $(this).unbind( '_delayedBind-' + encEvent + '-' + timeout, callback );
65                 } );
66         }
67 } );
68 } )( jQuery );