yui-later.js revision b39897a381c2203466da5568bfd2862a54a81311
c4e6d94ea429e473a6732b6eb5e0fc980e822881Adam MooreYUI.add('yui-later', function(Y) {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore/**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Provides a setTimeout/setInterval wrapper
87d6b0a14cce52c4faa4b78fc9878eb553dab0d5Adam Moore * @module yui
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @submodule yui-later
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore */
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore(function() {
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore var L = Y.Lang,
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore
87d6b0a14cce52c4faa4b78fc9878eb553dab0d5Adam Moore /**
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * Executes the supplied function in the context of the supplied
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * object 'when' milliseconds later. Executes the function a
87d6b0a14cce52c4faa4b78fc9878eb553dab0d5Adam Moore * single time unless periodic is set to true.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @method later
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @for YUI
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param when {int} the number of milliseconds to wait until the fn
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * is executed.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param o the context object.
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * @param fn {Function|String} the function to execute or the name of
1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749Adam Moore * the method in the 'o' object to execute.
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore * @param data [Array] data that is provided to the function. This
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore * accepts either a single item or an array. If an array is provided,
9fb523cf517ad4d6a53ae9f461d672cba63835d2Adam Moore * the function is executed with one parameter for each array item.
9fb523cf517ad4d6a53ae9f461d672cba63835d2Adam Moore * If you need to pass a single array parameter, it needs to be wrapped
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore * in an array [myarray].
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore * @param periodic {boolean} if true, executes continuously at supplied
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore * interval until canceled.
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore * @return {object} a timer object. Call the cancel() method on this
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore * object to stop the timer.
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore */
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore later = function(when, o, fn, data, periodic) {
87d6b0a14cce52c4faa4b78fc9878eb553dab0d5Adam Moore when = when || 0;
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore var m = fn, f, id;
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore if (o && L.isString(fn)) {
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore m = o[fn];
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore }
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore f = !L.isUndefined(data) ? function() {
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore m.apply(o, Y.Array(data));
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore } : function() {
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore m.call(o);
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore };
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore id = (periodic) ? setInterval(f, when) : setTimeout(f, when);
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore return {
91ff24e65531ce8bf171340d9384182f8c168af3Adam Moore id: id,
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore interval: periodic,
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore cancel: function() {
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore if (this.interval) {
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore clearInterval(id);
87d6b0a14cce52c4faa4b78fc9878eb553dab0d5Adam Moore } else {
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore clearTimeout(id);
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore }
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore }
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore };
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore };
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore Y.later = later;
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore L.later = later;
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore})();
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore
87d6b0a14cce52c4faa4b78fc9878eb553dab0d5Adam Moore
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore}, '@VERSION@' ,{requires:['yui-base']});
d4dbc3afb5bb9cfd13490b358dc37bf951104ca7Adam Moore