commit 59b77f98936681a0624fb15e0ae7a0f3fe1aad23 Author: turuslan Date: Mon Jun 29 02:54:41 2015 +0500 Initial commit diff --git a/HackTimer.js b/HackTimer.js new file mode 100644 index 0000000..a21cade --- /dev/null +++ b/HackTimer.js @@ -0,0 +1,64 @@ +(function (workerScript) { + var worker, + fakeIdToCallback = {}; + if (typeof (Worker) !== 'undefined') { + function getFakeId (prefix) { + return prefix + Math.floor( + Math.random () * + Number.MAX_VALUE + ); + } + try { + worker = new Worker (workerScript); + window.setInterval = function (callback, time) { + var fakeId = getFakeId ('i-'); + fakeIdToCallback[fakeId] = callback; + worker.postMessage ({ + name: 'setInterval', + fakeId: fakeId, + time: time + }); + return fakeId; + }; + window.clearInterval = function (fakeId) { + delete fakeIdToCallback[fakeId]; + worker.postMessage ({ + name: 'clearInterval', + fakeId: fakeId + }); + }; + window.setTimeout = function (callback, time) { + var fakeId = getFakeId ('t-'); + fakeIdToCallback[fakeId] = callback; + worker.postMessage ({ + name: 'setTimeout', + fakeId: fakeId, + time: time + }); + return fakeId; + }; + window.clearTimeout = function (fakeId) { + delete fakeIdToCallback[fakeId]; + worker.postMessage ({ + name: 'clearTimeout', + fakeId: fakeId + }); + }; + worker.onmessage = function (event) { + var data = event.data, + fakeId = data.fakeId, + callback = fakeIdToCallback[fakeId]; + callback.call (window); + }; + worker.onerror = function (event) { + console.log(event); + }; + console.log('HackTimer.js by turuslan: Initialisation succeeded'); + } catch (error) { + console.log('HackTimer.js by turuslan: Initialisation failed'); + console.error(error); + } + } else { + console.log('HackTimer.js by turuslan: Initialisation failed - HTML5 Web Worker is not supported'); + } +})('HackTimerWorker.js'); \ No newline at end of file diff --git a/HackTimer.min.js b/HackTimer.min.js new file mode 100644 index 0000000..a70a822 --- /dev/null +++ b/HackTimer.min.js @@ -0,0 +1 @@ +(function(s){var w,f={},o=window,m=Math;if(typeof(Worker)!=='undefined'){function g(p){return p+m.floor(m.random()*Number.MAX_VALUE);}try{w=new Worker(s);o.setInterval=function(c,t){var i=g('i-');f[i]=c;w.postMessage({n:'a',i:i,t:t});return i;};o.clearInterval=function(i){delete f[i];w.postMessage({n:'b',i:i});};o.setTimeout=function(c,t){var i=g('t-');f[i]=c;w.postMessage({n:'c',i:i,t:t});return i;};o.clearTimeout=function(i){delete f[i];w.postMessage({n:'d',i:i});};w.onmessage=function(e){f[e.data.i].call(o);};w.onerror=function(e){console.log(e);};console.log('HackTimer.js by turuslan: Initialisation succeeded');}catch(e){console.log('HackTimer.js by turuslan: Initialisation failed');console.error(e);}}else{console.log('HackTimer.js by turuslan: Initialisation failed - HTML5 Web Worker is not supported');}})('HackTimerWorker.min.js'); \ No newline at end of file diff --git a/HackTimerWorker.js b/HackTimerWorker.js new file mode 100644 index 0000000..6b6c33c --- /dev/null +++ b/HackTimerWorker.js @@ -0,0 +1,30 @@ +var fakeIdToId = {}; +onmessage = function (event) { + var data = event.data, + name = data.name, + fakeId = data.fakeId, + time; + if(data.hasOwnProperty('time')) { + time = data.time; + } + switch (name) { + case 'setInterval': + fakeIdToId[fakeId] = setInterval(function () { + postMessage({fakeId: fakeId}); + }, time); + break; + case 'clearInterval': + clearInterval(fakeIdToId[fakeId]); + delete fakeIdToId[fakeId]; + break; + case 'setTimeout': + fakeIdToId[fakeId] = setTimeout(function () { + postMessage({fakeId: fakeId}); + }, time); + break; + case 'clearTimeout': + clearTimeout(fakeIdToId[fakeId]); + delete fakeIdToId[fakeId]; + break; + } +} diff --git a/HackTimerWorker.min.js b/HackTimerWorker.min.js new file mode 100644 index 0000000..c9c6278 --- /dev/null +++ b/HackTimerWorker.min.js @@ -0,0 +1 @@ +var f={};onmessage=function(e){var d=e.data,i=d.i,t=d.hasOwnProperty('t')?d.t:0;switch(d.n){case'a':f[i]=setInterval(function(){postMessage({i:i});},t);break;case'b':clearInterval(f[i]);delete f[i];break;case'c':f[i]=setTimeout(function(){postMessage({i:i});},t);break;case'd':clearTimeout(f[i]);delete f[i];break;}} \ No newline at end of file