mirror of
https://github.com/turuslan/HackTimer
synced 2024-11-21 23:19:11 +00:00
Clearing timeout after timeout callback ( https://github.com/turuslan/HackTimer/issues/11#issuecomment-159768029 ). Limit and cycle fake id to 31 bit unsigned integer range, prevent overwriting of active fake id.
This commit is contained in:
parent
70ff2be207
commit
e11f3ed229
15
HackTimer.js
15
HackTimer.js
@ -47,10 +47,17 @@ onmessage = function (event) {\
|
||||
var worker,
|
||||
fakeIdToCallback = {},
|
||||
lastFakeId = 0,
|
||||
maxFakeId = 0x7FFFFFFF, // 2 ^ 31 - 1, 31 bit, positive values of signed 32 bit integer
|
||||
logPrefix = 'HackTimer.js by turuslan: ';
|
||||
if (typeof (Worker) !== 'undefined') {
|
||||
function getFakeId () {
|
||||
lastFakeId ++;
|
||||
while (fakeIdToCallback.hasOwnProperty (lastFakeId)) {
|
||||
if (lastFakeId == maxFakeId) {
|
||||
lastFakeId = 0;
|
||||
} else {
|
||||
lastFakeId ++;
|
||||
}
|
||||
}
|
||||
return lastFakeId;
|
||||
}
|
||||
try {
|
||||
@ -81,7 +88,8 @@ onmessage = function (event) {\
|
||||
var fakeId = getFakeId ();
|
||||
fakeIdToCallback[fakeId] = {
|
||||
callback: callback,
|
||||
parameters: Array.prototype.slice.call(arguments, 2)
|
||||
parameters: Array.prototype.slice.call(arguments, 2),
|
||||
isTimeout: true
|
||||
};
|
||||
worker.postMessage ({
|
||||
name: 'setTimeout',
|
||||
@ -109,6 +117,9 @@ onmessage = function (event) {\
|
||||
request = fakeIdToCallback[fakeId];
|
||||
callback = request.callback;
|
||||
parameters = request.parameters;
|
||||
if (request.hasOwnProperty ('isTimeout') && request.isTimeout) {
|
||||
delete fakeIdToCallback[fakeId];
|
||||
}
|
||||
}
|
||||
if (typeof (callback) === 'string') {
|
||||
try {
|
||||
|
2
HackTimer.min.js
vendored
2
HackTimer.min.js
vendored
@ -1 +1 @@
|
||||
(function(s){var w,f={},o=window,l=console,m=Math,z='postMessage',x='HackTimer.js by turuslan: ',v='Initialisation failed',p=0,r='hasOwnProperty',y=[].slice,b=Worker;try{s=o.URL.createObjectURL(new Blob(["var f={},p=postMessage,r='hasOwnProperty';onmessage=function(e){var d=e.data,i=d.i,t=d[r]('t')?d.t:0;switch(d.n){case'a':f[i]=setInterval(function(){p(i)},t);break;case'b':if(f[r](i)){clearInterval(f[i]);delete f[i]}break;case'c':f[i]=setTimeout(function(){p(i);if(f[r](i))delete f[i]},t);break;case'd':if(f[r](i)){clearTimeout(f[i]);delete f[i]}break}}"]))}catch(e){}if(typeof(b)!=='undefined'){try{w=new b(s);o.setInterval=function(c,t){var i=++p;f[i]={c:c,p:y.call(arguments,2)};w[z]({n:'a',i:i,t:t});return i};o.clearInterval=function(i){if(f[r](i))delete f[i],w[z]({n:'b',i:i})};o.setTimeout=function(c,t){var i=++p;f[i]={c:c,p:y.call(arguments,2)};w[z]({n:'c',i:i,t:t});return i};o.clearTimeout=function(i){if(f[r](i))delete f[i],w[z]({n:'d',i:i})};w.onmessage=function(e){var n=e.data,c;if(f[r](n))n=f[n],c=n.c;if(typeof(c)=='string')try{c=new Function(c)}catch(k){l.log(x+'Error parsing callback code string: ',k)}if(typeof(c)=='function')c.apply(o,n.p)};w.onerror=function(e){l.log(e)};l.log(x+'Initialisation succeeded')}catch(e){l.log(x+v);l.error(e)}}else l.log(x+v+' - HTML5 Web Worker is not supported')})('HackTimerWorker.min.js');
|
||||
(function(s){var w,f={},o=window,l=console,m=Math,z='postMessage',x='HackTimer.js by turuslan: ',v='Initialisation failed',p=0,r='hasOwnProperty',y=[].slice,b=Worker,a=0x7FFFFFFF;function d(){while(f[r](p))p=a>p?p+1:0;return p}try{s=o.URL.createObjectURL(new Blob(["var f={},p=postMessage,r='hasOwnProperty';onmessage=function(e){var d=e.data,i=d.i,t=d[r]('t')?d.t:0;switch(d.n){case'a':f[i]=setInterval(function(){p(i)},t);break;case'b':if(f[r](i)){clearInterval(f[i]);delete f[i]}break;case'c':f[i]=setTimeout(function(){p(i);if(f[r](i))delete f[i]},t);break;case'd':if(f[r](i)){clearTimeout(f[i]);delete f[i]}break}}"]))}catch(e){}if(typeof(b)!=='undefined'){try{w=new b(s);o.setInterval=function(c,t){var i=d();f[i]={c:c,p:y.call(arguments,2)};w[z]({n:'a',i:i,t:t});return i};o.clearInterval=function(i){if(f[r](i))delete f[i],w[z]({n:'b',i:i})};o.setTimeout=function(c,t){var i=d();f[i]={c:c,p:y.call(arguments,2),t:!0};w[z]({n:'c',i:i,t:t});return i};o.clearTimeout=function(i){if(f[r](i))delete f[i],w[z]({n:'d',i:i})};w.onmessage=function(e){var i=e.data,c,n;if(f[r](i)){n=f[i];c=n.c;if(n[r]('t'))delete f[i]}if(typeof(c)=='string')try{c=new Function(c)}catch(k){l.log(x+'Error parsing callback code string: ',k)}if(typeof(c)=='function')c.apply(o,n.p)};w.onerror=function(e){l.log(e)};l.log(x+'Initialisation succeeded')}catch(e){l.log(x+v);l.error(e)}}else l.log(x+v+' - HTML5 Web Worker is not supported')})('HackTimerWorker.min.js');
|
Loading…
Reference in New Issue
Block a user