Created
June 22, 2011 20:13
-
-
Save insin/1041031 to your computer and use it in GitHub Desktop.
Object vs. function singleton
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
| 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); |
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
| // 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