Killing a JavaScript function which runs infinite

≯℡__Kan透↙ 提交于 2020-07-03 06:34:12

问题


As an example

var runInfinite = function(){
    while(1)
    {
       // Do stuff;
    }
};

setTimeout(runInfinite, 0);

Is it possible to break this runInfinite function form running infinite? I mean is it possible to kill this function from another function without using a flag or return statement?


回答1:


The answer is no. Since JavaScript is single-threaded ( unless you are using some less common implementation which I doubt ) nothing can break a loop ( or any other block of code ) from outside.




回答2:


There is no direct way to "kill" a running javascript function.

Possible workaround, although you need to replace the while(1) loop:

var i = 0; // for testing purposes

var toCall = "runInfinite()";
function runInfinite(){
    // do stuff
    console.log(i++);
    setTimeout(function(){ eval(toCall); }, 100); // or whatever timeout you want
}

setTimeout(function(){ eval(toCall); }, 0); // start the function
setTimeout(function(){ toCall = ""; }, 5000); // "kill" the function

I know using eval() is considered to be bad practice, however the idea should be clear. The function calls itself (not recursive, therefore the setTimeout()) using the toCall variable. Once this variable is unset, you kill it from outside.

You could wrap these variables and functions in a class so you can implement your "kill" function.




回答3:


I've just tested on my browser.

AFAIK, the while(1) block will literally block the thread and not allow anything else to run. I don't have sources other than experience, but using the Chrome developer toolkit thing ctrl+shift+i typing while(1){} will block everything else due to the fact that browser-javascript is single threaded.

However, if you're using node.js, you may be able to as it has multithreading capabilities. If anyone is familiar enough with node.js and is willing to answer/edit, go for it. I've never quite used it.




回答4:


The whay you want not really. But maybe you're doing it wrong. Try not to use while(1) if possible and at the end of the function call the function again if the outside flag triggered but some break button is not set, or return if otherwise




回答5:


return can be used to stop a function from its current execution but not in your case.

In your case you could try to your function are wrapped in a try / catch block, you can find it out when your function gonna infinite ,based on that you can terminate its current execution.




回答6:


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())


来源:https://stackoverflow.com/questions/14870602/killing-a-javascript-function-which-runs-infinite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!