Open In App

JQuery callbacks.locked() Method

Last Updated : 24 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The callbacks.locked() method in jQuery is used to answer whether the callbacks list has been locked or not.

Syntax:

callbacks.locked()

Return Value: This method returns a boolean value.

Example 1: In this example, the callbacks have been locked so the method returns true.

html
<!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.remove() method
    </p>

    <button onclick="Geeks();">
        click here
    </button>
    
    <p id="GFG"></p>

    <script>
        var el_down = document
                .getElementById("GFG");

        var res = "";
        var callbacks = jQuery.Callbacks();

        function Geeks() {
            var func = function (val) {
                res = res + 
                "value passed is - " + val;
            };

            // Function added to list
            callbacks.add(func);
            callbacks.fire("gfg_1");

            // Locking the callback list
            callbacks.lock();
            
            // Checking using this method
            el_down.innerHTML = callbacks.locked();
        } 
    </script>
</body>

</html>

Output:

lockd1

lockd1


Example 2: This example provides a button to lock the list and then call the method to see the result.

html
<!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.remove() method
    </p>

    <button onclick="Geeks();">
        click here
    </button>
    
    <button onclick="lock();">
        lock here
    </button>
    
    <p id="GFG"></p>

    <script>
        var el_down = document.getElementById("GFG");
        var res = "";
        var callbacks = jQuery.Callbacks();

        // Defining lock function 
        function lock() {
            callbacks.lock();
        }
        function Geeks() {

            // Function to be added to the list
            var fun = function (val) {
                res = res + "This is function "
                    + "and value passed is " 
                    + val + "<br>";
            };
            
            // Adding
            callbacks.add(fun);
            callbacks.fire("GFG_1");
            el_down.innerHTML = callbacks.locked();
        } 
    </script>
</body>

</html>

Output:

lockd2

lockd2



Next Article

Similar Reads