Killing a JavaScript function which runs infinite

前端 未结 6 471
难免孤独
难免孤独 2021-01-19 00:04

As an example

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

setTimeout(runInfinite, 0);

Is it possible to

相关标签:
6条回答
  • 2021-01-19 00:27

    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

    0 讨论(0)
  • 2021-01-19 00:31

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

    0 讨论(0)
  • 2021-01-19 00:35

    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.

    0 讨论(0)
  • 2021-01-19 00:39

    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.

    0 讨论(0)
  • 2021-01-19 00:47

    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.

    0 讨论(0)
  • 2021-01-19 00:48

    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.

    0 讨论(0)
提交回复
热议问题