Created
April 18, 2016 09:21
-
-
Save h3xxx/ee6969aa4a1a0cbb58ebb2e03156e072 to your computer and use it in GitHub Desktop.
Timeouts pool take from http://stackoverflow.com/questions/315078/how-do-you-handle-multiple-instances-of-settimeout
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 Timeout = { | |
_timeouts: {}, | |
set: function(name, func, time){ | |
this.clear(name); | |
this._timeouts[name] = {pending: true, func: func}; | |
var tobj = this._timeouts[name]; | |
tobj.timeout = setTimeout(function() | |
{ | |
/* setTimeout normally passes an accuracy report on some browsers, this just forwards that. */ | |
tobj.func.call(arguments); | |
tobj.pending = false; | |
}, time); | |
}, | |
hasRun: function(name) | |
{ | |
if( this._timeouts[name] ) | |
{ | |
return !this._timeouts[name].pending; | |
} | |
return -1; /* Whut? */ | |
}, | |
runNow: function(name) | |
{ | |
if( this._timeouts[name] && this.hasRun(name)===false ) | |
{ | |
this._timeouts[name].func(-1); /* fake time. *shrug* */ | |
this.clear(name); | |
} | |
} | |
clear: function(name) | |
{ | |
if( this._timeouts[name] && this._timeouts[name].pending ) | |
{ | |
clearTimeout(this._timeouts[name].timeout); | |
this._timeouts[name].pending = false; | |
} | |
} | |
}; | |
Timeout.set("doom1", function(){ | |
if( Timeout.hasRun("doom2") === true ) | |
{ | |
alert("OMG, it has teh run"); | |
} | |
}, 2000 ); | |
Timeout.set("doom2", function(){ | |
/* NooP! */ | |
}, 1000 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment