zoukankan      html  css  js  c++  java
  • promise规范学习

    /*
    ecma6 实现了原生的js实现规范
    解决异步加载的问题
    */

    /*
    es6中原生的promise如何使用
    状态 -》 pending等待 -》resolve 通过状态 -》 reject 拒绝状态
    then方法 调用 resolve 和 reject 状态所对应的回调函数
    */

    用法:

    jquery中的用法

         <script>
    /*
       jquery中的deferred
     */
          $(function()
          {
              function show1(time)
               {
                       // reject可省略
                       var dfd = $.Deferred();
                           setTimeout(function(){
                          console.log(time+'秒后输出');
                          //reject();
                          dfd.resolve();
                          },time*1000)
                   return dfd;
                      
               }
               show1(1).then(function(){
                 console.log('成功')
               },function(){
                 console.log('失败')
               })
               //========================
               function p1(time)
               {
                       // reject可省略
                       var dfd = $.Deferred();
                           setTimeout(function(){
                          console.log(time+'秒后输出');
                          //reject();
                          dfd.resolve();
                          },time*1000)
                   return dfd;
                      
               }
                function p2(time)
               {
                       // reject可省略
                       var dfd = $.Deferred();
                           setTimeout(function(){
                          console.log(time+'秒后输出');
                          //reject();
                          dfd.resolve();
                          },time*1000)
                   return dfd;
                      
               }
                function p3(time)
               {
                       // reject可省略
                       var dfd = $.Deferred();
                           setTimeout(function(){
                          console.log(time+'秒后输出');
                          //reject();
                          dfd.resolve();
                          },time*1000)
                   return dfd;
                      
               }
               $.when(p1(1),p2(2),p3(3)).then(function(){
                    console.log('集合成功')
               },function(){
                     console.log('集合失败')
               })
          })
            
         </script>

    用例:

  • 相关阅读:
    flex>导航
    flex>图表控件
    Struts2>Cannot find the tag library descriptor for /strutstags
    jsp>Smartupload例子代码
    flex>MXML语法
    解决JBoss只能通过localhost(127.0.0.1)而不能通过IP访问
    jsp>tomcat配置虚拟目录
    JSF>概述
    Struts2>中文乱码
    flex>HttpService
  • 原文地址:https://www.cnblogs.com/h5monkey/p/6634785.html
Copyright © 2011-2022 走看看