如何让 JavaScript 等到某个事件发生?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6902334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:40:57  来源:igfitidea点击:

How to let JavaScript wait until certain event happens?

javascripteventsweb-applicationswait

提问by zw324

I am writing a webpage with the following structure:

我正在编写一个具有以下结构的网页:

  1. One section (table A) depends on another section (table B);
  2. Another section (table B) has elements that require recalculation on each update. The calculation is handled by external tools, and will cause an event when finished.
  1. 一个部分(表 A)依赖于另一部分(表 B);
  2. 另一部分(表 B)包含需要在每次更新时重新计算的元素。计算由外部工具处理,完成后将引发事件。

In order to guarantee correctness, the table need to be updated only after the other table is fully updated (i.e., done with computation). However, I don't know how to effectively achieve this, and I could not find any waitfacility within JavaScript.

为了保证正确性,只有在另一个表完全更新(即完成计算)后才需要更新该表。但是,我不知道如何有效地实现这一点,而且我wait在 JavaScript 中找不到任何工具。

For now, I am using the following method:

目前,我使用以下方法:

  1. Declare a global variable updatedand make it false;
  2. After the first table received input, I make an empty whileloop until updatedis true;
  3. Add an listener, once the calculation is done and the event received, set updatedto true.
  1. 声明一个全局变量updated并创建它false
  2. 在第一个表收到输入后,我做一个空while循环,直到updatedtrue
  3. 添加一个监听器,一旦计算完成并接收updated到事件,设置为true

This seems unintuitive to me but I cannot think of any other way of doing it. Is there any good ways to do this?

这对我来说似乎不直观,但我想不出任何其他方式来做到这一点。有什么好的方法可以做到这一点吗?

Thanks for any inputs!

感谢您的任何投入!

采纳答案by evan

Add an listener, once the calculation is done and the event received, set updated to true.

添加一个监听器,一旦计算完成并接收到事件,将更新设置为 true。

Instead of setting updatedto true, and then waiting for updatedto be true- just do whatever you want to do in the listener.

而不是设置updated为真,然后等待为真updated- 只需在侦听器中做任何您想做的事情。

myEventBus.addListener(function () {
    // do whatever
    updateTable();
    alert('table updated!');
});

回答by hugomg

Doing empty while loops is a bad idea. Not only do you burn CPU cycles, but Javacript is single threaded so you will loop forever without giving anyone a chance to change the variable.

做空 while 循环是一个坏主意。您不仅会消耗 CPU 周期,而且 Javacript 是单线程的,因此您将永远循环,而不会给任何人更改变量的机会。

What you can do is rewrite the table that has other people depending on it to "fire an event itself". There are many ways to do this, but basicaly you just want it to call a "continuation' function instead of blindily returning. This function can be predefined or you can pass it as a parameter somewhere.

您可以做的是重写有其他人依赖它的表以“触发事件本身”。有很多方法可以做到这一点,但基本上你只是想让它调用一个“继续”函数而不是盲目地返回。这个函数可以被预定义,或者你可以将它作为参数传递给某个地方。

//this is just illustrative
//Your actual code will be probably very different from this.

function update_part(){
    //do something
    signal_finished_part()
}

var parts_done = 0;
function signal_finished_part(){
    parts_done ++;
    if(parts_done >= 5){
        signal_all_parts_done();
    }
}

function signal_all_parts_done()
{
    //do something to table A
}

回答by Soham Bhaumik

You could write a callback function for whatever triggers the update. To avoid messy callbacks, you could use promises too, and update parts of the table depending on the data retrieved in the update operation. Open to suggestions.

您可以为触发更新的任何内容编写回调函数。为了避免混乱的回调,您也可以使用承诺,并根据更新操作中检索到的数据更新表的部分内容。对建议持开放态度。