Created
April 27, 2015 13:16
-
-
Save ScottKaye/e9a2c4666e92e054c739 to your computer and use it in GitHub Desktop.
Timeout - will run a function and forcibly terminate after a given time.
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
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