Skip to content

Instantly share code, notes, and snippets.

@founddrama
Forked from devongovett/gist:1572498
Created January 7, 2012 03:29
Show Gist options
  • Save founddrama/1573682 to your computer and use it in GitHub Desktop.
Save founddrama/1573682 to your computer and use it in GitHub Desktop.
/*
* 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);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment