• ajax同步,加载loading的bug


    https://blog.csdn.net/educast/article/details/71422502

    https://blog.csdn.net/web_xyk/article/details/52292183

    版权声明:本文为博主原创文章,未经博主允许不得转载。转载请标明出处:http://blog.csdn.net/ligang2585116! https://blog.csdn.net/ligang2585116/article/details/51589073
    在讲述Promise时,曾提及过Deferred对象。下面内容,详细阐述Deferred对象及其用法。

    一、为什么使用Deferred对象
    目前,大部分浏览器已经支持原生Promise写法,但对于IE这种“古板”的浏览器我们只能硬着头皮去兼容它。Github中提供了好多插件去支持。例如babel-polyfill。但是其对目前主流前端构建工具兼容性不是很好(比如Grunt)。所以,难道我们只能去放弃Promise???当然不是,Deferred对象就是一个很好的替代方案。

    二、什么是Deferred对象
    defer,推迟;延期。含义就是”延迟”到未来某个点再执行。
    在开发中,我们经常遇到某些耗时很长的javascript操作。其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组),它们都不是立即能得到结果的。通常的做法是,为它们指定回调函数(callback)。即事先规定,一旦它们运行结束,应该调用哪些函数。但是,一旦回调层级过深,处理和维护会变得相当困难。jQuery开发团队就设计了deferred对象,来作为回调函数的解决方案。

    三、ajax操作的链式写法
    $.ajax({
    url: "http://localhost:8888",
    success: function(){
    console.log("哈哈,成功了!");
    },
    error: function(){
    console.log("出错啦!");
    }
    });
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $.ajax()操作完成后,如果使用的是低于1.5.0版本的jQuery,返回的是XHR对象,你没法进行链式操作;如果高于1.5.0版本,返回的是deferred对象,可以进行链式操作。

    $.ajax("http://localhost:8888")
    .done(function(){
    console.log("哈哈,成功了!");
    })
    .fail(function(){
    console.log("出错啦!");
    });
    1
    2
    3
    4
    5
    6
    7
    四、同一操作指定多个回调函数
    deferred对象允许自由添加多个回调函数。

    $.ajax("http://localhost:8888")
    .done(function(){
    console.log("哈哈,成功了!");
    })
    .fail(function(){
    console.log("出错啦!");
    })
    .done(function(){
    console.log("第二个成功回调函数!");
    });
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    注意:回调函数可以添加任意多个,其按照添加顺序执行。

    五、多个操作指定同一回调函数
    deferred对象允许为多个事件指定一个回调函数

    $.when($.ajax("http://localhost:8888"),$.ajax("http://localhost:9999"))
    .done(function(){
    console.log("哈哈,成功了!");
    })
    .fail(function(){
    console.log("出错啦!");
    });
    1
    2
    3
    4
    5
    6
    7
    上述示例:如果都成功了,就运行done()指定的回调函数;
    如果有一个失败或都失败了,就执行fail()指定的回调函数。

    六、本地操作使用回到函数
    deferred对象,把这一套回调函数接口,从ajax操作扩展到了所有操作。

    var wait = function(){
    var tasks = function(){
    console.log("wait函数执行完毕!");
    };
    setTimeout(tasks, 5000);
    };
    1
    2
    3
    4
    5
    6
    我们想,在wait执行完,输出”哈哈,成功了!”

    由于jQuery.when(deferreds)方法只能接收defferred对象作为参数,所以我们需对上述wait改写!

    var dtd = $.Deferred(); // 新建一个Deferred对象
    var wait = function(dtd){
    var tasks = function(){
    console.log("wait函数执行完毕!");
    dtd.resolve(); // 改变Deferred对象的执行状态
    };
    setTimeout(tasks, 5000);
    return dtd; // 返回deferred对象
    };
    $.when(wait(dtd))
    .done(function(){
    console.log("哈哈,成功了!");
    })
    .fail(function(){
    console.log("出错了!");
    });
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    上述示例中,提及到了deferred.resolve()。说明其作用需要先说一下jQuery规定deferred对象的三种执行状态:未完成、已完成和已失败。

    如果执行状态是”已完成”(resolved),deferred对象立刻调用done()方法指定的回调函数;
    如果执行状态是”已失败”,调用fail()方法指定的回调函数;
    如果执行状态是”未完成”,则继续等待,或者调用progress()方法指定的回调函数(jQuery1.7版本添加)。
    七、deferred.promise()方法
    上面示例实现了我们的要求:在wait执行完,输出”哈哈,成功了!”
    但是作为JavaScript忍者,我们是不允许这样抒写的,因为dtd是一个全局变量,它的执行状态可以从外部改变。

    var dtd = $.Deferred(); // 新建一个Deferred对象
    var wait = function(dtd){
    var tasks = function(){
    console.log("wait函数执行完毕!");
    dtd.resolve(); // 改变Deferred对象的执行状态
    };
    setTimeout(tasks, 5000);
    return dtd; // 返回deferred对象
    };
    $.when(wait(dtd))
    .done(function(){
    console.log("哈哈,成功了!");
    })
    .fail(function(){
    console.log("出错了!");
    });
    dtd.resolve();
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    在代码的尾部加了一行dtd.resolve(),这就改变了dtd对象的执行状态,因此导致done()方法立刻执行,跳出”哈哈,成功了!”的提示框,等5秒之后再跳出”wait函数执行完毕!”的提示框。

    解决上述“全局变量”问题:我们使用deferred.promise()
    其作用:在原来的deferred对象上返回Promise对象,后者只开放与改变执行状态无关的方法(比如done()方法和fail()方法),屏蔽与改变执行状态有关的方法(比如resolve()方法和reject()方法),从而使得执行状态不能被改变。

    图:deffered.png

    图:deferred.promise().png

    方式一:deferred.promise

    var wait = function(){
    var dtd = $.Deferred();
    var tasks = function(){
    console.log("wait函数执行完毕!");
    dtd.resolve(); // 改变Deferred对象的执行状态
    };
    setTimeout(tasks, 5000);
    return dtd.promise(); // 返回Promise对象
    };
    $.when(wait())
    .done(function(){
    console.log("哈哈,成功了!");
    })
    .fail(function(){
    console.log("出错了!");
    });
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    方式二:在wait对象上部署deferred接口

    var dtd = $.Deferred(); // 生成Deferred对象
    var wait = function(dtd){
    var tasks = function(){
    console.log("执行完毕!");
         dtd.resolve(); // 改变Deferred对象的执行状态
      };
      setTimeout(tasks,5000);
    };
    // 在wait对象上部署Deferred接口
    dtd.promise(wait);
    wait.done(function(){
    console.log("哈哈,成功了!");
    }).fail(function(){
    console.log("出错啦!");
    });
    wait(dtd); // 调用wait函数
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    八、deferred对象的方法
    $.Deferred() 生成一个deferred对象。
    deferred.done(function(){}) 指定操作成功时的回调函数
    deferred.fail(function(){}) 指定操作失败时的回调函数
    deferred.promise()
    没有参数时,返回一个新的deferred对象,该对象的运行状态无法被改变;
    接受参数时,作用为在参数对象上部署deferred接口。
    deferred.resolve() 手动改变deferred对象的运行状态为”已完成”,从而立即触发done()方法。
    deferred.reject() 手动改变deferred对象的运行状态变为”已失败”,从而立即触发fail()方法。
    $.when() 为多个操作指定回调函数。
    deferred.then() 有时为了省事,可以把done()和fail()合在一起写,这就是then()方法。
    $.when($.ajax("/main.php" ))
    .then(successFunc, failureFunc );
    1
    2
    如果then()有两个参数,那么第一个参数是done()方法的回调函数,第二个参数是fail()方法的回调方法。如果then()只有一个参数,那么等同于done()。
    9. deferred.always()这个方法也是用来指定回调函数的,它的作用是,不管调用的是deferred.resolve()还是deferred.reject(),最后总是执行。

    $.ajax("test.html")
    .always(function(){
    console.log("已执行!");
    });
    ---------------------
    作者:奋飛
    来源:CSDN
    原文:https://blog.csdn.net/ligang2585116/article/details/51589073?utm_source=copy
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    接口请求的例子
    SQLServer跨库查询分布式查询
    查看数据库估计的执行计划
    数据库对比工具
    十步完成windows服务的安装
    cdn 浏览器缓存机制
    quartz spring 整合例子
    利用jquery 实现多文件上传
    监测数据库的结构变化
    yii sql的输出
  • 原文地址:https://www.cnblogs.com/beimingbingpo/p/9778018.html
Copyright © 2020-2023  润新知