diff options
author | Adrian Lang <lang@cosmocode.de> | 2009-12-01 12:50:19 +0100 |
---|---|---|
committer | Adrian Lang <lang@cosmocode.de> | 2010-01-12 12:45:36 +0100 |
commit | 21ed602531cbceff3e92e4ac33484f44ed2b6848 (patch) | |
tree | 64e137287ae404e04908173ac55600df1ad8f301 /lib/scripts/delay.js | |
parent | 98dba5ad053f051c0291c9ac9b291c783ebed372 (diff) | |
download | dokuwiki-21ed602531cbceff3e92e4ac33484f44ed2b6848.tar.gz dokuwiki-21ed602531cbceff3e92e4ac33484f44ed2b6848.zip |
Factor out timer and delay management
darcs-hash:20091201115019-e4919-fe83e3d69eb997d0c04064b46117690824fe4daf.gz
Diffstat (limited to 'lib/scripts/delay.js')
-rw-r--r-- | lib/scripts/delay.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/scripts/delay.js b/lib/scripts/delay.js new file mode 100644 index 000000000..2ef9f8846 --- /dev/null +++ b/lib/scripts/delay.js @@ -0,0 +1,69 @@ +/** + * Manage delayed and timed actions + * + * @license GPL2 (http://www.gnu.org/licenses/gpl.html) + * @author Adrian Lang <lang@cosmocode.de> + */ + +/** + * Provide a global callback for window.setTimeout + * + * To get a timeout for non-global functions, just call + * delay.add(func, timeout). + */ +var timer = { + _cur_id: 0, + _handlers: {}, + + execDispatch: function (id) { + timer._handlers[id](); + }, + + add: function (func, timeout) { + var id = ++timer._cur_id; + timer._handlers[id] = func; + return window.setTimeout('timer.execDispatch(' + id + ')', timeout); + } +}; + +/** + * Provide a delayed start + * + * To call a function with a delay, just create a new Delay(func, timeout) and + * call that object’s method “start”. + */ +function Delay (func, timeout) { + this.func = func; + if (timeout) { + this.timeout = timeout; + } +} + +Delay.prototype = { + func: null, + timeout: 500, + + delTimer: function () { + if (this.timer !== null) { + window.clearTimeout(this.timer); + this.timer = null; + } + }, + + start: function () { + this.delTimer(); + var _this = this; + this.timer = timer.add(function () { _this.exec.call(_this); }, + this.timeout); + + this._data = { + _this: arguments[0], + _params: Array.prototype.slice.call(arguments, 2) + }; + }, + + exec: function () { + this.delTimer(); + this.func.call(this._data._this, this._data._params); + } +}; |