w3resource

jQuery: Lock a callback list in its current state

jQuery Fundamental - I : Exercise-50

Lock a callback list in its current state.

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Lock a callback list in its current state.</title>
</head>
<body>
</body>
</html>

JavaScript Code :


// A sample function f1 to be added to a callbacks list
var f1 = function( value ) {
  console.log( "f1:" + value );
};
 
var callbacks = $.Callbacks();
 
// Add f1 to the callback list
callbacks.add( f1 );
 
// Fire the items on the list, passing an argument
callbacks.fire( "JavaScript" );


// Lock the callbacks list
callbacks.lock();
 
// Try firing the items again
callbacks.fire( "jQuery" );

//As the list was locked, no items were called,
// so "jQuery" isn't logged

See the Pen jquery-fundamental-exercise-50 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Check if a callback list contains a specific callback.
Next: jQuery Fundamental - II exercises

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.