简体   繁体   English

如何在JavaScript中中断和停止正在运行的函数?

[英]How can I interrupt and stop a running function in JavaScript?

I have several buttons and each calculates some numbers and displays it on screen. 我有几个按钮,每个按钮计算一些数字并在屏幕上显示。 Different buttons take differnet amount of time to finish calculation. 不同的按钮需要不同的时间来完成计算。 In my current implementation, if I click on a button and then click on a different button before the first one finishes, the result from the second button is displayed only after the first one finishes. 在我当前的实现中,如果我单击按钮然后在第一个按钮完成之前单击另一个按钮,则第二个按钮的结果仅在第一个按钮完成后显示。 How can I modify this that as soon as I click the second button, it stops the calculation initiated by the first button and continues with its calculation? 如何修改它,一旦我单击第二个按钮,它会停止第一个按钮启动的计算并继续计算?

HTML HTML

<input type="button" value="One" onclick="displayNumber(1)">
<input type="button" value="Two" onclick="displayNumber(2)">
....

JavaScript JavaScript的

function displayNumber(id) {
    alert(calculateAwesomeNumber(id));
}

This may be not possbile in JavaScript as it is single-threaded and only one function can be running at any time. 这在JavaScript中可能是不可能的,因为它是单线程的,并且任何时候只能运行一个函数。 Am I correct? 我对么?

You can use WebWorkers . 您可以使用WebWorkers

This will work in a separate thread and allow you to do heavy calculations without intefering with the UI thread. 这将在一个单独的线程中工作,允许您在不干扰UI线程的情况下进行繁重的计算。

You can signal the worker to stop. 你可以发信号通知工人停下来。

See here for examples on how to use: 请参阅此处以获取有关如何使用的示例:
http://www.html5rocks.com/en/tutorials/workers/basics/ http://www.html5rocks.com/en/tutorials/workers/basics/
https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers

If WebWorkers isn't an option you can use a flag to force the function to stop: 如果WebWorkers不是一个选项,您可以使用标志强制该函数停止:

var _stop;

function displayNumber(num) {

    _stop = false;

    while(!_stop) {
        //calc
    }

}

And then on button2 click: 然后在按钮2上单击:

<input type="button" value="Two" onclick="_stop=true;displayNumber(2)">

Instead of a loop you can check _stop after each segment of the calculation is done. 在计算的每个段完成后,您可以检查_stop而不是循环。

Just note that if the calculation is long and busy you might not get a response of the button until it finishes. 请注意,如果计算时间很长并且很忙,则可能无法获得按钮的响应,直到完成为止。 You can use setTimout(..., 0) from time to time inside the calculation to allow other events to execute. 您可以在计算中不时使用setTimout(..., 0)以允许执行其他事件。

How you implement this will depend on the code you use for calculation. 如何实现这将取决于您用于计算的代码。

You can break up the calculation for example like this: 您可以分解计算,例如:

function displayNumber(num) {

    var forCalculation;

    function step1() {
        //calc step 1 you have access to forCalculation and num vars here
        if (_stop) return;
        setTimeout(step2, 0);
    }

    function step2() {
        //calc step 2 you have access to forCalculation and num vars here
        if (_stop) return;
        setTimeout(step3, 0);
    }
    function step3() {
        //calc step 3 you have access to forCalculation and num vars here
        if (_stop) return;
    }
    step1();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM