Skip to content

Instantly share code, notes, and snippets.

@ppalludan
Last active August 29, 2015 14:15
Show Gist options
  • Save ppalludan/63e679f1855df099063b to your computer and use it in GitHub Desktop.
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
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