我有两个异步函数,第一个触发事件 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/