【问题标题】:How to await ALL of the Listeners execution for Custom jQuery Event?如何等待自定义 jQuery 事件的所有侦听器执行?
【发布时间】:2020-05-01 21:45:44
【问题描述】:
$(document).on('custom-event', listener1 );
$(document).on('custom-event', listener2 );

function listener1() {
    return new Promise((resolve) => {
        setTimeout(resolve, 3000);
    })
}

function listener2() {
    return new Promise((resolve) => {
        setTimeout(resolve, 10000);
    })
}


$(document).trigger('custom-event');
const afterAwaiting = true; // Need to await 10 seconds after previous line of code to execute this statement

我已经触发了自定义事件 - custom-event

需要等待所有的监听器执行其代码,

然后继续执行下一条语句。

【问题讨论】:

    标签: javascript jquery events async-await listener


    【解决方案1】:

    我使用when找到了解决方案。

    await $.when( $(document).trigger('custom-event') )
    
    const afterAwaiting = true;
    

    jQuery 运行自定义事件 Synchronously 根据 问题

    jQuery trigger custom event synchronously?

    或者即使没有await也可以工作

    $(document).trigger('custom-event')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      相关资源
      最近更新 更多