Created
January 6, 2012 21:35
-
-
Save devongovett/1572498 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Non-clamped setInterval | |
* By Devon Govett (idea from sink.js) | |
* MIT LICENSE | |
*/ | |
(function() { | |
var BlobBuilder = this.BlobBuilder || this.MozBlobBuilder || this.WebKitBlobBuilder, | |
URL = this.URL || this.webkitURL, | |
Worker = this.Worker; | |
this.createTimer = function(fn, interval) { | |
if (!BlobBuilder || !URL || !Worker) | |
return setInterval(fn, interval); | |
var bb = new BlobBuilder; | |
bb.append("setInterval(function() { postMessage('ping'); }, " + interval + ");"); | |
var url = URL.createObjectURL(bb.getBlob()); | |
var worker = new Worker(url); | |
worker.onmessage = fn; | |
worker.url = url; | |
return worker; | |
} | |
this.destroyTimer = function(timer) { | |
if (timer.terminate) { | |
timer.terminate(); | |
URL.revokeObjectURL(timer.url); | |
} | |
else { | |
clearInterval(timer); | |
} | |
} | |
})(); |
Oh right, forgot about the Firefox 8 issue, and I didn't bother with the whole dataURL thing since it will still work in Opera, but using regular setInterval
. Luckily the Firefox 8 thing is easy to work around, so I'll add that later today probably.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This "silently" fails on Firefox 8 and doesn't achieve the wanted result on Opera (it only supports workers from Data URIs). :(
That's why sink.js does it using this: https://gist.github.com/1431080 , feature-testing before using and using the best working alternative (Blob, Data, setInterval).