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