Mik*_*ier 4 javascript jquery events synchronization
假设我正在做3个ajax调用,我想在做某事之前等待3个调用完成.
是否有一个库可以在JavaScript中同步多个异步事件?(使用或不使用jQuery的事件系统)
例:
var sync = new syncLib();
$('a1').click(sync.newListener());
$('a2').click(sync.newListener());
sync.wait(function(e1, e2) {
// fired when both a1 and a2 are clicked or when 10 seconds have passed
// e1 and e2 would have properties to know whether or not they timed out or not..
}, 10 /* timeout */));
Run Code Online (Sandbox Code Playgroud)
我找到了这个:https://github.com/Ovea/js-sync/blob/master/README.md,但不支持超时.(假设第二个ajax调用花了太长时间,我不希望我的同步被挂起,我想设置一个10秒的超时)
我知道我可以自己编写代码,但我只是在这里查看(在google搜索之后)
谢谢!
编辑: 从那时起我发现async:https://github.com/caolan/async
Esa*_*ija 11
$.when($.ajax("/"), $.ajax("/"), $.ajax("/")).then(function () {
alert("all 3 requests complete");
});
Run Code Online (Sandbox Code Playgroud)