/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* Thi Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Invokes periodically a {@param} <code class="param">callback</code> every {@param} <code class="param">frequency</code> seconds.<br/>
* Partially based upon prototype.js PeriodicalExecuter
* @namespace jsbeans
* @class Repeater
* @constructor
* @param callback {Function}
* @param frequency {Integer} interval in seconds between two subsequent calls
* @param [args] {Object} arguments for callback
* */
jsbeans.Repeater = function(callback, frequency /*, arguments*/) {
this.callback = callback;
this.frequency = frequency;
this.args = arguments;
};
/**
* Starts repeating
* @method start
* */
jsbeans.Repeater.prototype.start = function() {
var args = new Array();
for (var i = 2; i < this.args.length; i++) {
args.push(this.args[i]);
}
this.args = args;
this.callback.apply(this.callback, args);
var _currentlyExecuting = this._currentlyExecuting;
var callback = this.callback;
var _starter = function() {
if (!_currentlyExecuting) {
try {
_currentlyExecuting = true;
callback.apply(callback, args);
}
finally {
_currentlyExecuting = false;
}
}
};
this._timerId = window.setInterval(_starter, this.frequency * 1000);
};
/**
* Stops repeating.
* @method stop
* */
jsbeans.Repeater.prototype.stop = function() {
window.clearInterval(this._timerId);
};
/**
* After this call every Function will be added with a "repeatExtension" method.<br/>
* Sample usage
* <pre>
* function myFun() { // my code here }
* myFun.repeatExtension(5000, 3) // execute myFun 3 times every 5 seconds
* myFun.repeatExtension(5000, -1, 'ciccio') // exeute myFun every 5 seconds forever passing string "ciccio" as argument of myFun
* </pre>
* @method all
* @static
*/
jsbeans.Repeater.all = function() {
Function.prototype.repeatExtension = function () {
var s = this;
var _args;
if (typeof arguments[0].callee != 'undefined'){
_args = arguments[0];
}
else {
_args = arguments;
}
var delay = _args[0];
var timesToExecute = _args[1];
this._timerId = null;
var args = new Array();
for (var i = 2; i < _args.length; i++) {
args.push(_args[i]);
}
s.apply(this, args);
if (this._timerId) {
clearTimeout(this._timerId);
}
if (timesToExecute == -1 || --timesToExecute > 0) {
this._timerId = setTimeout(function (){
_args[1] = timesToExecute;
s.repeatExtension(_args);
}, delay);
}
return s;
};
};
/**
* Instance method for static one.<br/>
* It internally calls {@method} <code class="methd">jsbeans.Repeater.all()</code>
* @method all
* */
jsbeans.Repeater.prototype.all = function() {
jsbeans.Repeater.all();
};