Created
November 18, 2011 18:44
-
-
Save henriquemoody/1377345 to your computer and use it in GitHub Desktop.
Watcher in javascript.
This file contains 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
var Watcher = function () | |
{ | |
var _interval = 1000; | |
var _running = false; | |
var _callback = function () | |
{ | |
// Just a callback | |
}; | |
var _watcher = this; | |
var _runner = function () | |
{ | |
if (true === _watcher.isRunning()) { | |
_watcher.getCallback().call(null); | |
window.setTimeout(_runner, _watcher.getInterval()); | |
} | |
}; | |
this.getCallback = function () | |
{ | |
return _callback; | |
}; | |
this.setCallback = function (callback) | |
{ | |
if (typeof(callback) !== 'function') { | |
throw 'Invalid callback.'; | |
} | |
_callback = callback; | |
return this; | |
}; | |
this.getInterval = function () | |
{ | |
return _interval; | |
}; | |
this.setInterval = function (interval) | |
{ | |
if (isNaN(interval)) { | |
throw 'Invalid number'; | |
} | |
_interval = interval; | |
return this; | |
}; | |
this.isRunning = function () | |
{ | |
return _running; | |
}; | |
this.start = function () | |
{ | |
_running = true; | |
_runner(); | |
return this; | |
}; | |
this.stop = function () | |
{ | |
_running = false; | |
return this; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment