X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=utils%2Fjalviewjs%2Fsite-resources%2Fjavascript%2Fjquery.timer.js;fp=utils%2Fjalviewjs%2Fsite-resources%2Fjavascript%2Fjquery.timer.js;h=fe9afe2e8bf6decedd6f298eda5f24eaf7ff5dee;hb=a83adb45bdf9554e270921b4baad94defd314b36;hp=0000000000000000000000000000000000000000;hpb=d4ec118f86b5c9dee801e743c46aaacc7bb521d1;p=jalview.git diff --git a/utils/jalviewjs/site-resources/javascript/jquery.timer.js b/utils/jalviewjs/site-resources/javascript/jquery.timer.js new file mode 100644 index 0000000..fe9afe2 --- /dev/null +++ b/utils/jalviewjs/site-resources/javascript/jquery.timer.js @@ -0,0 +1,75 @@ +/* + * + * jQuery Timer plugin v0.1 + * Matt Schmidt [http://www.mattptr.net] + * + * Licensed under the BSD License: + * http://mattptr.net/license/license.txt + * + */ + + jQuery.timer = function (interval, callback) + { + /** + * + * timer() provides a cleaner way to handle intervals + * + * @usage + * $.timer(interval, callback); + * + * + * @example + * $.timer(1000, function (timer) { + * alert("hello"); + * timer.stop(); + * }); + * @desc Show an alert box after 1 second and stop + * + * @example + * var second = false; + * $.timer(1000, function (timer) { + * if (!second) { + * alert('First time!'); + * second = true; + * timer.reset(3000); + * } + * else { + * alert('Second time'); + * timer.stop(); + * } + * }); + * @desc Show an alert box after 1 second and show another after 3 seconds + * + * + */ + + var interval = interval || 100; + + if (!callback) + return false; + + _timer = function (interval, callback) { + this.stop = function () { + clearInterval(self.id); + }; + + this.internalCallback = function () { + callback(self); + }; + + this.reset = function (val) { + if (self.id) + clearInterval(self.id); + + var val = val || 100; + this.id = setInterval(this.internalCallback, val); + }; + + this.interval = interval; + this.id = setInterval(this.internalCallback, this.interval); + + var self = this; + }; + + return new _timer(interval, callback); + }; \ No newline at end of file