聆听JavaScript中事件的缺失

时间:2019-08-23 10:52:24

标签: javascript

第三方JS插件发出一个可以监听的事件:

jsInt.on('ready', function(services) {
    if(Array.isArray(services) && ~services.indexOf('magic')) {
        //integration is ready to use and it has 'magic' service available
    } else {
        //integration is ready to use but it does not have 'magic' service
    }
});

如何以编程方式检测3秒钟内是否没有发生此事件?

2 个答案:

答案 0 :(得分:4)

on回调取消一个超时,该超时在3秒后运行:

const timeoutId = setTimeout(() => {
  console.log('Library did not load in time!');
  // ...handle the problem
}, 3000);

jsInt.on('ready', function(services) {
  clearTimeout(timeoutId);

  // rest of the code
  if(Array.isArray(services) && ~services.indexOf('magic')) {
    //integration is ready to use and it has 'magic' service available
  } else {
    //integration is ready to use but it does not have 'magic' service
  }
});

答案 1 :(得分:0)

这就是诺言。

<p>