javascript - jQuery 等待两个触发器

标签 javascript jquery asynchronous jquery-events eventtrigger

我有两个异步函数,第一个触发事件 1,第二个在完成时触发事件 2。

我只想在触发两个事件后才执行我的第三个函数。 我怎样才能以简单的方式做到这一点?

最佳答案

我们也有类似的,问题可能不完全一样。我们使用 JQuery 提供的 $.when

https://api.jquery.com/jquery.when/

这个例子是从上面的链接复制过来的

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
  // a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
  // Each argument is an array with the following structure: [ data, statusText, jqXHR ]
  var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"
  if ( /Whip It/.test( data ) ) {
    alert( "We got what we came for!" );
  }
});

关于javascript - jQuery 等待两个触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50520560/

相关文章:

javascript - Vue.js 2 - 使用事件中心从数组中删除项目

javascript - cropper.js 有问题

jquery - 在 jQuery 中检索动态生成的文本区域的值?

php - 如何获得不同行的不同求和值

Javascript 禁用鼠标光标并仅使用键盘光标

javascript - 缩小网页以适应 iframe 尺寸

javascript - 取消ajax()成功

c# - 哪个线程运行 `await` 关键字之后的代码?

javascript - JavaScript(异步)如何在继续之前确定 "wait"的用途?

c# - Task.WhenAny ContinueWith : Get argument?