Created
February 23, 2010 20:38
-
-
Save bhauman/312675 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is a port of the prototype.js PeriodcialExecuter | |
| // This should be framework independent | |
| // Valuable if you like to/have to program in jQuery | |
| function PeriodicalExecuter(callback, frequency) { | |
| this.callback = callback; | |
| this.frequency = frequency; | |
| this.currentlyExecuting = false; | |
| this.registerCallback(); | |
| } | |
| PeriodicalExecuter.prototype = { | |
| registerCallback: function() { | |
| var me = this; | |
| this.timer = setInterval(function() { me.onTimerEvent() }, this.frequency * 1000); | |
| }, | |
| execute: function() { | |
| this.callback(this); | |
| }, | |
| stop: function() { | |
| if (!this.timer) return; | |
| clearInterval(this.timer); | |
| this.timer = null; | |
| }, | |
| onTimerEvent: function() { | |
| if (!this.currentlyExecuting) { | |
| try { | |
| this.currentlyExecuting = true; | |
| this.execute(); | |
| } finally { | |
| this.currentlyExecuting = false; | |
| } | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment