Skip to content

Instantly share code, notes, and snippets.

@henriquemoody
Created November 18, 2011 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henriquemoody/1377345 to your computer and use it in GitHub Desktop.
Save henriquemoody/1377345 to your computer and use it in GitHub Desktop.
Watcher in javascript.
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