当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。