Last active
August 29, 2015 14:15
-
-
Save ppalludan/63e679f1855df099063b to your computer and use it in GitHub Desktop.
Implemention of a "run once" function, so that I can invoke a given method like crazy, and the method is only invoked once
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 Singleton = { | |
_threads : [], | |
process: function (name, callback) { | |
var evt = this._threads[name]; | |
if (evt == null) { | |
evt = this._threads[name] = { name : name }; | |
} | |
if (evt.running) { | |
evt.que = true; | |
return; | |
} | |
function complete() { | |
evt.running = false; | |
if (evt.que) { | |
evt.que = false; | |
evt.running = true; | |
return callback(complete); | |
} | |
} | |
evt.running = true; | |
return callback(complete); | |
} | |
} | |
var count = 0; | |
function test1() { | |
Singleton.process('name', function (callback) { | |
var timer = setTimeout(function () { dosomeshit(); callback(); }, 2000); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment