Open In App

How to check lock-state of a callback list using jQuery ?

Improve
Improve
Like Article
Like
Save
Share
Report

In jQuery, a callback list is an object that stores a list of functions to be executed when a specific event occurs. These functions are called “callbacks”. Callbacks are often used in jQuery to perform certain actions after an event has occurred.

However, sometimes it may be necessary to check the lock-state of a callback list in order to determine whether it is safe to add or remove a callback from the list. In this article, we will look at how to check the lock-state of a callback list using jQuery.

Approach: To check the lock-state of a callback list in jQuery, we can use the $.Callbacks() method. This method creates a new callback list that can be locked or unlocked. When a callback list is locked, it cannot be modified. This means that no new callbacks can be added or removed from the list. On the other hand, when a callback list is unlocked, it can be modified as needed.

The $.Callbacks().locked() method returns a boolean value indicating whether the callback list is locked or not. If the callback list is locked, it returns true, and if it is unlocked, it returns false.

Syntax:

$.Callbacks().locked();

Example 1: In this example, we will create a callback list and check its lock-state using the $.Callbacks().locked() method. We will display the lock-state of the callback list on the page using a message.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
      </script>
    <script>
        $(document).ready(function () {
            var callbacks = $.Callbacks();
  
            // Check lock-state of callback list
            if (callbacks.locked()) {
                $("#message").text("Callback list is locked");
            } else {
                $("#message").text("Callback list is unlocked");
            }
        });
    </script>
  
    <style>
        body {
            margin: 0;
            padding: 0;
            display: grid;
            place-content: center;
            background-color: #F0EBCE;
        }
  
  
        ::selection {
            color: white;
            background-color: green;
        }
  
        #message {
            border: 2px dotted black;
            margin: 20px;
            padding: 10px;
            width: fit-content;
            font: 20px;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
  
    <p>How to check the lock-state of 
       a callback list using jQuery?
    </p>
    <div id="message"></div>
</body>
  
</html>


Output: The output of this code will be a message on the page that says “Callback list is unlocked”.

 

Example 2: In this example, we will create a callback list, lock it, and then check its lock-state using the $.Callbacks().locked() method. We will display the lock-state of the callback list on the page using a message.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
      </script>
    <script>
        $(document).ready(function () {
            var callbacks = $.Callbacks();
  
            // Lock callback list
            callbacks.lock();
  
            // Check lock-state of callback list
            if (callbacks.locked()) {
                $("#message").text("Callback list is locked");
            } else {
                $("#message").text("Callback list is unlocked");
            }
        });
    </script>
  
    <style>
        body {
            margin: 0;
            padding: 0;
            display: grid;
            place-content: center;
            background-color: black;
            color: white;
        }
  
        ::selection {
            color: white;
            background-color: green;
        }
  
        #message {
            border: 2px dotted white;
            margin: 20px;
            padding: 10px;
            width: fit-content;
            font: 20px;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <p>How to check the lock-state of a
       callback list using jQuery?
    </p>
    <div id="message"></div>
</body>
  
</html>


Output: The output of this code will be a message on the page that says “Callback list is locked”.

 



Last Updated : 11 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads