JQuery deferred.then()方法

JQuery deferred.then()方法

JQuery中的这个deferred.then()方法是用来添加处理程序,当递延对象被解决、拒绝或正在进行时,将被调用。

语法:

deferred.then(doneCallbacks[, failCallbacks][, progressCallbacks])

参数:

  • doneCallbacks。这是一个函数,或一个函数数组,当递延解决时被调用。
  • failCallbacks。这是一个函数,或一个函数数组,当递延被拒绝时被调用。
  • progressCallbacks。这是一个函数,或一个函数数组,当进度通知被发送到递延对象时被调用。

返回值:该方法方法返回递延对象。

下面讨论两个例子。

  • 例子。在这个例子中,then()方法与notify和resolve方法被调用。
<!DOCTYPE HTML> 
<html>  
<head> 
    <title> 
      JQuery | deferred.then() method
    </title>
    <script src="https://code.jquery.com/jquery-3.5.0.js">
</script> 
</head>   
<body style="text-align:center;">
    <h1 style="color:green;">  
        GeeksForGeeks  
    </h1> 
    <p id="GFG_UP"> 
    </p>
    <button onclick = "Geeks();">
    click here
    </button>
    <p id="GFG_DOWN"> 
    </p>
    <script> 
        var el_up = document.getElementById("GFG_UP");
        el_up.innerHTML = "JQuery | deferred.then() method";
        function Func1(val, div){
          (div).append("From doneCallbacks - " + val);
        }
        function Func2(val, div){
          (div).append("From failCallbacks - " + val);
        }
        function Func3(val, div){
          (div).append("From progressCallbacks - " + val);
        }
        function Geeks() {
            var def =.Deferred();
            def.then(Func1, Func2, Func3);
            def.notify('Deferred "def" is notified.<br/>'
                     , '#GFG_DOWN');
            def.resolve('Deferred "def" is resolved.<br/>'
                     , '#GFG_DOWN');
        } 
    </script> 
</body>   
</html>         
  • 输出:
    JQuery deferred.then()方法

  • 例子。在这个例子中,then()方法被调用,并带有通知和拒绝方法。

<!DOCTYPE HTML> 
<html>  
<head> 
    <title> 
      JQuery | deferred.then() method
    </title>
    <script src="https://code.jquery.com/jquery-3.5.0.js">
    </script> 
</head>   
<body style="text-align:center;">
    <h1 style="color:green;">  
        GeeksForGeeks  
    </h1> 
    <p id="GFG_UP"> 
    </p>
    <button onclick = "Geeks();">
    click here
    </button>
    <p id="GFG_DOWN"> 
    </p>
    <script> 
        var el_up = document.getElementById("GFG_UP");
        el_up.innerHTML = "JQuery | deferred.then() method";
        function Func1(val, div){
          (div).append("From doneCallbacks - " + val);
        }
        function Func2(val, div){
          (div).append("From failCallbacks - " + val);
        }
        function Func3(val, div){
          (div).append("From progressCallbacks - " + val);
        }
        function Geeks() {
            var def =.Deferred();
            def.then(Func1, Func2, Func3);
            def.notify('Deferred "def" is notified.<br/>',
                       '#GFG_DOWN');
            def.reject('Deferred "def" is rejected.<br/>',
                       '#GFG_DOWN');
        } 
    </script> 
</body>   
</html> 
  • 输出:
    JQuery deferred.then()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程