|
1 |
| -# [jQuery Timeout Plugin](https://github.com/tkem/jquery-timeout/) |
| 1 | +# [jQuery Timeout Plugin](https://github.com/tkem/jquery-timeout) |
2 | 2 |
|
3 |
| -**jQuery 1.5** introduced the [Deferred |
4 |
| -Object](http://api.jquery.com/category/deferred-object/) to ease |
5 |
| -handling callbacks for asynchronous events. Although the jQuery |
6 |
| -documentation contains examples on how to use Deferred objects with |
7 |
| -the native `window.setTimeout()` and `window.clearTimeout()` |
8 |
| -functions, jQuery does not provide a simple interface for timer-based |
9 |
| -Deferreds yet. |
| 3 | +**jQuery 1.5** introduced the [Deferred Object][1] to register |
| 4 | +callbacks for asynchronous events. Although the jQuery documentation |
| 5 | +contains examples on how to use Deferred objects with the native |
| 6 | +[`window.setTimeout()`][2] and [`window.clearTimeout()`][3] functions, |
| 7 | +jQuery does not provide a simple interface for timer-based Deferreds |
| 8 | +yet. |
10 | 9 |
|
11 |
| -This plugin provides the two functions `jQuery.timeout()` and |
12 |
| -`jQuery.timeoutWith()` that, given a delay in milliseconds, create a |
13 |
| -`jQuery.Deferred` instance that will be resolved after the given |
14 |
| -delay. The returned Promise object also provides `clear()` and |
15 |
| -`clearWith()` methods, which will reject the Deferred object and clear |
16 |
| -the native timeout ID, if called before the timeout has elapsed. |
| 10 | +This plugin provides the two functions [`jQuery.timeout()`](#timeout) |
| 11 | +and [`jQuery.timeoutWith()`](#timeoutWith) that create a Deferred |
| 12 | +object which is resolved after a given delay. The Deferred's Promise |
| 13 | +object provides additional methods [`clear()`](#clear) and |
| 14 | +[`clearWith()`](#clearWith), which will reject the Deferred and clear |
| 15 | +the native timeout ID if called before the delay has elapsed. |
17 | 16 |
|
18 | 17 |
|
19 | 18 | ## API Documentation
|
20 | 19 |
|
21 |
| -### jQuery.timeout( delay [, args... ] ) |
| 20 | +### <a name="timeout"></a> jQuery.timeout( delay [, args... ] ) |
22 | 21 |
|
23 | 22 | Create a Deferred object that will be resolved after the specified
|
24 | 23 | `delay` in milliseconds. When the Deferred is resolved, any
|
25 | 24 | doneCallbacks are called with optional arguments `args`. The returned
|
26 | 25 | `Timeout` object extends the Deferred's Promise object with the
|
27 |
| -addional methods `clear()` and `clearWith()`, for rejecting the |
28 |
| -Deferred before the delay has elapsed. |
| 26 | +addional methods [`clear()`](#clear) and [`clearWith()`](#clearWith), |
| 27 | +for rejecting the Deferred before the delay has elapsed. |
29 | 28 |
|
30 |
| -### jQuery.timeoutWith( delay, context [, args ] ) |
| 29 | +### <a name="timeoutWith"></a> jQuery.timeoutWith( delay, context [, args ] ) |
31 | 30 |
|
32 | 31 | Create a Deferred object that will be resolved after the specified
|
33 | 32 | `delay` in milliseconds. When the Deferred is resolved, any
|
34 | 33 | doneCallbacks are called with the given `context` as the `this`
|
35 | 34 | object, and the optional array `args` as arguments. The returned
|
36 | 35 | `Timeout` object extends the Deferred's Promise object with the
|
37 |
| -addional methods `clear()` and `clearWith()`, for rejecting the |
38 |
| -Deferred before the delay has elapsed. |
| 36 | +addional methods [`clear()`](#clear) and [`clearWith()`](#clearWith), |
| 37 | +for rejecting the Deferred before the delay has elapsed. |
39 | 38 |
|
40 |
| -### timeout.clear( [ args... ] ) |
| 39 | +### <a name="clear"></a> timeout.clear( [ args... ] ) |
41 | 40 |
|
42 |
| -Clear a pending Promise object returned by `jQuery.timeout()` or |
43 |
| -`jQuery.timeoutWith()` by immediately rejecting the corresponding |
44 |
| -Deferred object. When the Deferred is rejected, any failCallbacks are |
45 |
| -called with optional arguments `args`. |
| 41 | +Clear a pending Promise object returned by |
| 42 | +[`jQuery.timeout()`](#timeout) or |
| 43 | +[`jQuery.timeoutWith()`](#timeoutWith) by immediately rejecting the |
| 44 | +corresponding Deferred object. When the Deferred is rejected, any |
| 45 | +failCallbacks are called with optional arguments `args`. |
46 | 46 |
|
47 |
| -### timeout.clearWith( context [, args ] ) |
| 47 | +### <a name="clearWith"></a> timeout.clearWith( context [, args ] ) |
48 | 48 |
|
49 |
| -Clear a pending Promise object returned by `jQuery.timeout()` or |
50 |
| -`jQuery.timeoutWith()` by immediately rejecting the corresponding |
51 |
| -Deferred object. When the Deferred is rejected, any failCallbacks are |
52 |
| -called with the given `context` as the `this` object, and the optional |
53 |
| -array `args` as arguments. |
| 49 | +Clear a pending Promise object returned by |
| 50 | +[`jQuery.timeout()`](#timeout) or |
| 51 | +[`jQuery.timeoutWith()`](#timeoutWith) by immediately rejecting the |
| 52 | +corresponding Deferred object. When the Deferred is rejected, any |
| 53 | +failCallbacks are called with the given `context` as the `this` |
| 54 | +object, and the optional array `args` as arguments. |
54 | 55 |
|
55 | 56 |
|
56 | 57 | ## Examples
|
57 | 58 |
|
58 | 59 | Invoke a callback function after a five-second delay:
|
59 | 60 |
|
60 |
| -``` |
61 |
| -$.timeout( 5000, $("#status"), "5 seconds" ).done(function( $e, msg ) { |
62 |
| - $e.text( msg + " later..." ); |
63 |
| -}); |
64 |
| -``` |
| 61 | + $.timeout( 5000, $("#status"), "5 seconds" ).done(function( $e, msg ) { |
| 62 | + $e.text( msg + " later..." ); |
| 63 | + }); |
65 | 64 |
|
66 |
| -Same as above, but pass the element as the context argument to |
67 |
| -`deferredWith`: |
| 65 | +Same as above, but pass the element as context argument to |
| 66 | +[`jQuery.timeoutWith()`](#timeoutWith): |
68 | 67 |
|
69 |
| -``` |
70 |
| -$.timeoutWith( 5000, $("#status"), [ "5 seconds" ] ).done(function( msg ) { |
71 |
| - this.text(msg + " later..."); |
72 |
| -}); |
73 |
| -``` |
| 68 | + $.timeoutWith( 5000, $("#status"), [ "5 seconds" ] ).done(function( msg ) { |
| 69 | + this.text(msg + " later..."); |
| 70 | + }); |
74 | 71 |
|
75 | 72 | Provide handlers to be called when the timeout is resolved (elapsed)
|
76 | 73 | or rejected (cleared), and an event handler to manually clear the
|
77 | 74 | timeout.
|
78 |
| - |
79 |
| -``` |
80 |
| -var timeout = $.timeout( 10000 ); |
81 |
| -timeout.then( |
82 |
| - function() { alert( "timeout elapsed" ) }, |
83 |
| - function() { alert( "timeout cleared" ) } |
84 |
| -); |
85 |
| -$( "#clear" ).click(function() { |
86 |
| - timeout.clear(); |
87 |
| -}); |
88 |
| -``` |
| 75 | + |
| 76 | + var timeout = $.timeout( 10000 ); |
| 77 | + timeout.then( |
| 78 | + function() { alert( "timeout elapsed" ) }, |
| 79 | + function() { alert( "timeout cleared" ) } |
| 80 | + ); |
| 81 | + $( "#clear" ).click(function() { |
| 82 | + timeout.clear(); |
| 83 | + }); |
| 84 | + |
| 85 | +[1]: http://api.jquery.com/category/deferred-object/ |
| 86 | +[2]: http://developer.mozilla.org/en/docs/Web/API/window.setTimeout |
| 87 | +[3]: http://developer.mozilla.org/en/docs/Web/API/window.clearTimeout |
0 commit comments