Skip to content

Instantly share code, notes, and snippets.

@insin
Created June 22, 2011 20:13
Show Gist options
  • Select an option

  • Save insin/1041031 to your computer and use it in GitHub Desktop.

Select an option

Save insin/1041031 to your computer and use it in GitHub Desktop.
Object vs. function singleton
var loader = (function() {
var index = 0
, timeout = null
, symbols = ['└', '├', '┌', '┬', '┐', '┤', '┘', '┴'];
function show () {
console.log(symbols[index]);
index = (index + 1) % symbols.length;
timeout = setTimeout(show, 200);
}
function hide() {
clearTimeout(timeout);
}
return {show: show, hide: hide};
})();
// Testing
loader.show();
setTimeout(loader.hide, 5000);
// From http://pastebin.com/tmZiMhCp
var loader = {
_index: 0,
_timeout: null,
_symbols: ['└', '├', '┌', '┬', '┐', '┤', '┘', '┴'],
show: function() {
var that = this;
console.log(this._symbols[this._index]);
this._index = this._index == this._symbols.length - 1 ? 0 : this._index + 1;
this._timeout = setTimeout(function() { that.show() }, 200);
},
hide: function() {
clearTimeout(this._timeout);
}
};
// Testing
loader.show();
setTimeout(function() { loader.hide(); }, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment