Killing a JavaScript function which runs infinite

前端 未结 6 475
难免孤独
难免孤独 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: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.

提交回复
热议问题