yui-later.js revision a76f066cb8261ad9f7c6c4d81d8c3bed2f1a788a
145N/AYUI.add('yui-later', function(Y) {
145N/A
145N/A/**
145N/A * Provides a setTimeout/setInterval wrapper. This module is a `core` YUI module, <a href="../classes/YUI.html#method_later">it's documentation is located under the YUI class</a>.
145N/A *
145N/A * @module yui
145N/A * @submodule yui-later
145N/A */
145N/A
145N/Avar NO_ARGS = [];
145N/A
145N/A/**
145N/A * Executes the supplied function in the context of the supplied
145N/A * object 'when' milliseconds later. Executes the function a
145N/A * single time unless periodic is set to true.
145N/A * @for YUI
145N/A * @method later
145N/A * @param when {int} the number of milliseconds to wait until the fn
145N/A * is executed.
145N/A * @param o the context object.
5680N/A * @param fn {Function|String} the function to execute or the name of
5680N/A * the method in the 'o' object to execute.
5685N/A * @param data [Array] data that is provided to the function. This
145N/A * accepts either a single item or an array. If an array is provided,
145N/A * the function is executed with one parameter for each array item.
145N/A * If you need to pass a single array parameter, it needs to be wrapped
187N/A * in an array [myarray].
3817N/A *
187N/A * Note: native methods in IE may not have the call and apply methods.
187N/A * In this case, it will work, but you are limited to four arguments.
187N/A *
5685N/A * @param periodic {boolean} if true, executes continuously at supplied
4194N/A * interval until canceled.
4194N/A * @return {object} a timer object. Call the cancel() method on this
1244N/A * object to stop the timer.
1258N/A */
1244N/AY.later = function(when, o, fn, data, periodic) {
4811N/A when = when || 0;
4811N/A data = (!Y.Lang.isUndefined(data)) ? Y.Array(data) : NO_ARGS;
4811N/A o = o || Y.config.win || Y;
4811N/A
4811N/A var cancelled = false,
187N/A method = (o && Y.Lang.isString(fn)) ? o[fn] : fn,
4811N/A wrapper = function() {
197N/A // IE 8- may execute a setInterval callback one last time
197N/A // after clearInterval was called, so in order to preserve
197N/A // the cancel() === no more runny-run, we have to jump through
197N/A // an extra hoop.
187N/A if (!cancelled) {
4811N/A if (!method.apply) {
187N/A method(data[0], data[1], data[2], data[3]);
4811N/A } else {
4811N/A method.apply(o, data || NO_ARGS);
4811N/A }
4811N/A }
4811N/A },
4811N/A id = (periodic) ? setInterval(wrapper, when) : setTimeout(wrapper, when);
4811N/A
4811N/A return {
4811N/A id: id,
145N/A interval: periodic,
1938N/A cancel: function() {
1938N/A cancelled = true;
4365N/A if (this.interval) {
4365N/A clearInterval(id);
4365N/A } else {
4365N/A clearTimeout(id);
4365N/A }
4365N/A }
4365N/A };
192N/A};
192N/A
145N/AY.Lang.later = Y.later;
145N/A
145N/A
187N/A
4365N/A
3294N/A}, '@VERSION@' ,{requires:['yui-base']});
187N/A