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


jQuery callbacks.fired()用法及代碼示例

jQuery callbacks.fired()方法用於檢查回調是否已被調用至少一次。此方法返回布爾值。

用法:

callbacks.fired()

參數:此方法不接受任何參數。

返回值:此方法返回一個布爾值。

範例1:由於已至少調用一次fire()方法,因此本示例返回true。



<!DOCTYPE HTML> 
<html> 
  
<head> 
    <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> 
        JQuery | callbacks.fired() method 
    </p> 
  
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <p id="GFG_DOWN"></p> 
  
    <script> 
        var el_down = document.getElementById("GFG_DOWN"); 
        var res = ""; 
        var callbacks = jQuery.Callbacks(); 
  
        function Geeks() { 
  
            // First function to be added to the list 
            var fun1 = function (val) { 
                res = res + "This is function 1 and" 
                    + " value passed is " + val + "<br>"; 
            }; 
  
            // Adding the function 1 
            callbacks.add(fun1); 
              
            // Calling with "GFG_1" 
            callbacks.fire("GFG_1"); 
              
            // Calling callbacks.fired() 
            // method to get true 
            el_down.innerHTML = callbacks.fired(); 
        }  
    </script> 
</body> 
  
</html>

輸出:

範例2:此示例返回false,因為尚未調用fire()方法。

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <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> 
        JQuery | callbacks.fired() method 
    </p> 
  
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <p id="GFG_DOWN"></p> 
  
    <script> 
        var el_down = document.getElementById("GFG_DOWN"); 
        var res = ""; 
        var callbacks = jQuery.Callbacks(); 
  
        function Geeks() { 
  
            // First function to be added to the list 
            var fun1 = function (val) { 
                res = res + "This is function 1 and" 
                + " value passed is " + val + "<br>"; 
            }; 
  
            // Adding the function 1 
            callbacks.add(fun1); 
              
            // Adding again 
            callbacks.add(fun1); 
              
            // Calling callbacks.fired() but  
            // This time we will get false 
            el_down.innerHTML = callbacks.fired(); 
        }  
    </script> 
</body> 
  
</html>

輸出:




相關用法


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