From ac37b8fe207cbabf4c044785d046ff00036fae4d Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 31 Jan 2012 18:14:09 +0100 Subject: [PATCH] Changed PeriodicalExecuter to shield from double execution Finally it's implementation is identically to Prototype's PeriodicalExecuter. Thank you guys for this fine solution! Closes #405 --- wcfsetup/install/files/js/WCF.js | 48 ++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index 34b349dd39..ec41edc29e 100644 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -893,6 +893,24 @@ WCF.Clipboard = { */ WCF.PeriodicalExecuter = function(callback, delay) { this.init(callback, delay); }; WCF.PeriodicalExecuter.prototype = { + /** + * callback for each execution cycle + * @var object + */ + _callback: null, + + /** + * interval id + * @var integer + */ + _intervalID: null, + + /** + * execution state + * @var boolean + */ + _isExecuting: false, + /** * Initializes a periodical executer. * @@ -900,21 +918,29 @@ WCF.PeriodicalExecuter.prototype = { * @param integer delay */ init: function(callback, delay) { - this.callback = callback; - this.delay = delay; - this.loop = true; + if (!$.isFunction(callback)) { + console.debug('[WCF.PeriodicalExecuter] Given callback is invalid, aborting.'); + return; + } - this.intervalID = setInterval($.proxy(this._execute, this), this.delay); + this._callback = callback; + this._intervalID = setInterval($.proxy(this._execute, this), delay); }, /** * Executes callback. */ _execute: function() { - this.callback(this); - - if (!this.loop) { - clearInterval(this.intervalID); + if (!this._isExecuting) { + try { + this._isExecuting = true; + this._callback(this); + this._isExecuting = false; + } + catch (e) { + this._isExecuting = false; + throw e; + } } }, @@ -922,7 +948,11 @@ WCF.PeriodicalExecuter.prototype = { * Terminates loop. */ stop: function() { - this.loop = false; + if (!this._intervalID) { + return; + } + + clearInterval(this._intervalID); } }; -- 2.20.1