Skip to content

Instantly share code, notes, and snippets.

@ScottKaye
Created April 27, 2015 13:16
Show Gist options
  • Save ScottKaye/e9a2c4666e92e054c739 to your computer and use it in GitHub Desktop.
Save ScottKaye/e9a2c4666e92e054c739 to your computer and use it in GitHub Desktop.
Timeout - will run a function and forcibly terminate after a given time.
function timeout(func, limit) {
var url = URL.createObjectURL(new Blob(
['(', func.toString(), ')()'], {
type: 'application/javascript'
}));
worker = new Worker(url);
URL.revokeObjectURL(url);
setTimeout(function () {
worker.terminate();
}, limit);
}
//Example
timeout(function () {
setInterval(function () {
console.log("This is a function that runs forever!");
}, 10);
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment