1 |
efrain |
1 |
YUI.add('yui-later', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Provides a setTimeout/setInterval wrapper. This module is a `core` YUI module,
|
|
|
5 |
* <a href="../classes/YUI.html#method_later">it's documentation is located under the YUI class</a>.
|
|
|
6 |
*
|
|
|
7 |
* @module yui
|
|
|
8 |
* @submodule yui-later
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
var NO_ARGS = [];
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Executes the supplied function in the context of the supplied
|
|
|
15 |
* object 'when' milliseconds later. Executes the function a
|
|
|
16 |
* single time unless periodic is set to true.
|
|
|
17 |
* @for YUI
|
|
|
18 |
* @method later
|
|
|
19 |
* @param when {Number} the number of milliseconds to wait until the fn
|
|
|
20 |
* is executed.
|
|
|
21 |
* @param o the context object.
|
|
|
22 |
* @param fn {Function|String} the function to execute or the name of
|
|
|
23 |
* the method in the 'o' object to execute.
|
|
|
24 |
* @param data [Array] data that is provided to the function. This
|
|
|
25 |
* accepts either a single item or an array. If an array is provided,
|
|
|
26 |
* the function is executed with one parameter for each array item.
|
|
|
27 |
* If you need to pass a single array parameter, it needs to be wrapped
|
|
|
28 |
* in an array [myarray].
|
|
|
29 |
*
|
|
|
30 |
* Note: native methods in IE may not have the call and apply methods.
|
|
|
31 |
* In this case, it will work, but you are limited to four arguments.
|
|
|
32 |
*
|
|
|
33 |
* @param periodic {boolean} if true, executes continuously at supplied
|
|
|
34 |
* interval until canceled.
|
|
|
35 |
* @return {object} a timer object. Call the cancel() method on this
|
|
|
36 |
* object to stop the timer.
|
|
|
37 |
*/
|
|
|
38 |
Y.later = function(when, o, fn, data, periodic) {
|
|
|
39 |
when = when || 0;
|
|
|
40 |
data = (!Y.Lang.isUndefined(data)) ? Y.Array(data) : NO_ARGS;
|
|
|
41 |
o = o || Y.config.win || Y;
|
|
|
42 |
|
|
|
43 |
var cancelled = false,
|
|
|
44 |
method = (o && Y.Lang.isString(fn)) ? o[fn] : fn,
|
|
|
45 |
wrapper = function() {
|
|
|
46 |
// IE 8- may execute a setInterval callback one last time
|
|
|
47 |
// after clearInterval was called, so in order to preserve
|
|
|
48 |
// the cancel() === no more runny-run, we have to jump through
|
|
|
49 |
// an extra hoop.
|
|
|
50 |
if (!cancelled) {
|
|
|
51 |
if (!method.apply) {
|
|
|
52 |
method(data[0], data[1], data[2], data[3]);
|
|
|
53 |
} else {
|
|
|
54 |
method.apply(o, data || NO_ARGS);
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
},
|
|
|
58 |
id = (periodic) ? setInterval(wrapper, when) : setTimeout(wrapper, when);
|
|
|
59 |
|
|
|
60 |
return {
|
|
|
61 |
id: id,
|
|
|
62 |
interval: periodic,
|
|
|
63 |
cancel: function() {
|
|
|
64 |
cancelled = true;
|
|
|
65 |
if (this.interval) {
|
|
|
66 |
clearInterval(id);
|
|
|
67 |
} else {
|
|
|
68 |
clearTimeout(id);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
};
|
|
|
72 |
};
|
|
|
73 |
|
|
|
74 |
Y.Lang.later = Y.later;
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
}, '3.18.1', {"requires": ["yui-base"]});
|