es5中判断多个ajax全部执行完成后进行回调
$.when() jQuery中的api
$.when($.ajax({ url: url, dataType: "jsonp", data: {"movieId": 248906}, success: function(data) { var data = data.data.data; var actorList = data.stars.split(',').join(' | '); var type = data.category.split(',').join(' | '); result.push({ "actorList": actorList, "type": type }) }, error: function(err) { console.log(err) } }), $.ajax({ url: url, dataType: "jsonp", data: {"movieId": 1212492}, success: function(data) { var data = data.data.data; var actorList = data.stars.split(',').join(' | '); var type = data.category.split(',').join(' | '); result.push({ "actorList": actorList, "type": type }) }, error: function(err) { console.log(err) } }), $.ajax({ url: url, dataType: "jsonp", data: {"movieId": 346765}, success: function(data) { var data = data.data.data; var actorList = data.stars.split(',').join(' | '); var type = data.category.split(',').join(' | '); result.push({ "actorList": actorList, "type": type }) }, error: function(err) { console.log(err) } })).done(function() { console.log(result) })
全局变量判断
var ajax_done_count = 0; function ajax_done() { if(ajax_done_count === 3) { // 渲染页面 } else { // 什么都不做 } } $.ajax({success: function(data) { ajax_done_count += 1; ajax_done(); }}) $.ajax({success: function(data) { ajax_done_count += 1; ajax_done(); }}) $.ajax({success: function(data) { ajax_done_count += 1; ajax_done(); }})
promise.all
貌似在es5中也支持promise了 ...我写了一个demo
var url = 'http://xxx/xxx'; var idList = [248906, 1212492, 346765, 341139]; var result = []; var promiseArr = []; function createPromise (id) { return new Promise(function(resolve, reject) { $.ajax({ url: url, dataType: "jsonp", data: {"movieId": id}, success: function(data) { var data = data.data.data; resolve({ "actorList": data.stars, "type": data.category }); }, error: function(err) { reject(err); } }) }) } idList.map(function(i) { promiseArr.push(createPromise(i)) }) console.log(promiseArr) Promise.all(promiseArr).then(function(data) { result = data; console.log(result); // 渲染页面 }).catch(function(err) { console.log(err); })
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05