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