當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


jQuery deferred.always()用法及代碼示例


jQuery中的deferred.always()方法用於添加處理程序,這些處理程序將在Deferred對象被解析或拒絕時被調用。指定的參數可以是單個函數或函數數組。

用法:

deferred.always( alwaysCallbacks [, alwaysCallbacks] )

參數:該方法接受上述和以下所述的兩個參數:

  • alwaysCallbacks:此參數指定一個函數或函數數組,在解析或拒絕Deferred時調用此函數。
  • alwaysCallbacks:此參數指定多個函數或函數數組,這些函數將在解析或拒絕Deferred時調用。它是一個可選參數。

返回值:此方法返回Deferred對象。

範例1:



<!DOCTYPE HTML> 
<html> 
  
<head> 
    <title> 
        JQuery deferred.always() 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> 
  
    <script> 
        var el_up = 
            document.getElementById("GFG_UP"); 
  
        el_up.innerHTML = 
            "JQuery | deferred.always() method"; 
  
        function Geeks() { 
  
            // Use the always() method to 
            // alert the user 
            $.get("testingGFG.php") 
                    .always(function () { 
                alert("Either $.get successfully" 
                    + " completed or error " 
                    + "callbacks arguments"); 
            }); 
        }  
    </script> 
</body> 
  
</html>

輸出:

  • 在單擊按鈕之前:
  • 單擊按鈕後:

範例2:

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <title> 
        JQuery | deferred.always() 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"); 
        var el_down = 
            document.getElementById("GFG_DOWN"); 
        el_up.innerHTML = 
            "JQuery | deferred.always() method"; 
  
        function Geeks() { 
  
            // Use the always() method to 
            // change the text 
            $.get("testingGFG.php").always(function () { 
                el_down.innerHTML = "Either $.get " 
                    + "successfully completed" + 
                    " or error callbacks arguments"; 
            }); 
        }  
    </script> 
</body> 
  
</html>

輸出:




相關用法


注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 jQuery deferred.always() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。