栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

jQuery Ajax防止延迟顺序循环失败

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jQuery Ajax防止延迟顺序循环失败

尝试

(function ($) {    $.when.all = whenAll;    function whenAll(arr) {        "use strict";        var deferred = new $.Deferred(), args = !! arr         ? $.isArray(arr)? arr: Array.prototype.slice.call(arguments) .map(function (p) {   return p.hasOwnProperty("promise")    ? p    : new $.Deferred()     .resolve(p, null, deferred.promise()) })         : [deferred.resolve(deferred.promise())], promises = {     "success": [],       "error": [] }, doneCallback = function (res) {     promises[this.state() === "resolved"    || res.textStatus === "success"    ? "success"    : "error"].push(res);     return (promises.success.length  + promises.error.length) === args.length  ? deferred.resolve(promises)  : res }, failCallback = function (res) {     // do `error` notification , processing stuff     // console.log(res.textStatus);     promises[this.state() === "rejected"   || res.textStatus === "error"   ? "error"   : "success"].push(res);     return (promises.success.length  + promises.error.length) === args.length  ? deferred.resolve(promises)  : res };        $.map(args, function (promise, index) { return $.when(promise).always(function (data, textStatus, jqxhr) {     return (textStatus === "success")          ? doneCallback.call(jqxhr, {  data: data,  textStatus: textStatus    ? textStatus    : jqxhr.state() === "resolved"      ? "success"      : "error",  jqxhr: jqxhr})          : failCallback.call(data, {  data: data,  textStatus: textStatus,  jqxhr: jqxhr}) })        });        return deferred.promise()    };}(jQuery));// returns `Object {//        success: Array[], //        error: Array[]//          }`

// e.g.,var request = function (url, data) {    return $.post(url, {        json: JSON.stringify(data)    })}, settings = [    ["/echo/json/", "success1"], // `success`    ["/echo/jsons/", "error1"], // `error`    ["/echo/json/", "success2"], // `success`    ["/echo/jsons/", "error2"], // `error`    ["/echo/json/", "success3"] // `success`];$.when.all(  $.map(settings, function (p) {    return request.apply($, p)  })).then(function (data) {    console.log(data);    // filter , process responses    $.each(data, function(key, value) {        if (key === "success") {          results.append(   "rn" + key + ":rn" + JSON.stringify(value, null, 4)          )        } else {results.append(   "rn" + key + ":rn"    + JSON.stringify(       value.map(function(v, k) {         v.data.responseText = $(v.data.responseText).filter("title, #summary, #explanation").text().replace(/s+/g, " ");         return v       })     , null, 4)          )        }    })}, function (e) {    console.log("error", e)});

jsfiddle
http://jsfiddle.net/guest271314/620p8q8h/



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/383001.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号