Skip to content

Commit

Permalink
Clearing timeout after timeout callback ( #11 (comment) ). Limit and …
Browse files Browse the repository at this point in the history
…cycle fake id to 31 bit unsigned integer range, prevent overwriting of active fake id.
  • Loading branch information
turuslan committed Dec 3, 2015
1 parent 70ff2be commit e11f3ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions HackTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion HackTimer.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 comments on commit e11f3ed

@turuslan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minified version updated

@turuslan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also reusing last fake id if it became free.

@turuslan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HackTimer.silent.min.js is out of date.

Please sign in to comment.