jQuery callbacks.fired()方法

jQuery callbacks.fired()方法

jQuery callbacks.fired()方法是用来检查回调是否已经被调用了至少一次。这个方法返回布尔值。

语法:

callbacks.fired()

参数:该方法不接受任何参数。

返回值:该方法返回一个布尔值。

例子1:这个例子返回真,因为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);
              
            // Calling with "GFG_1"
            callbacks.fire("GFG_1");
              
            // Calling callbacks.fired()
            // method to get true
            el_down.innerHTML = callbacks.fired();
        } 
    </script>
</body>
  
</html>

输出:

jQuery callbacks.fired()方法

例子2:这个例子返回错误,因为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>

输出:

jQuery callbacks.fired()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程