As an example
var runInfinite = function(){
while(1)
{
// Do stuff;
}
};
setTimeout(runInfinite, 0);
Is it possible to
I admit this is not exactly the same, but, there are the Javascript generators and Javascript iterators in ECMA-262. If you can replace your function with a generator function, then, you can implement the breakable feature easily.
function execWithTimeout(iter, timeout = 100) {
const limit = Date.now() + timeout
for (const output of iter) {
console.log(output)
if (Date.now() > limit) throw(new Error("Timeout reached"))
}
}
let runInfinite = function * () {
let i = 0
while (1) {
yield i++
}
}
execWithTimeout(runInfinite())