w3resource

jQuery: Check if a callback list contains a specific callback

jQuery Fundamental - I : Exercise-49

Check if a callback list contains a specific callback.

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>Check if a callback list contains a specific callback.</title>
</head>
<body>
</body>
</html>

JavaScript Code:

// A sample function f1 to be added to a callbacks list
var f1 = function( value1, value2 ) {
  console.log( "Received: " + value1 + "," + value2 );
};
 
// A second function f2  which will not be added to the list
var f2 = function( value1, value2 ) {
  console.log( "foobar" );
};
 
var callbacks = $.Callbacks();
 
// Add the function f1
callbacks.add( f1 );
 
//Find the callbacks which are in the list
console.log( callbacks.has( f1 ) );

console.log( callbacks.has( f2 ) );

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


Contribute your code and comments through Disqus.

Previous: Fire a list of callbacks with a specific context and an array of arguments.
Next: Lock a callback list in its current state.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/jquery-exercises/1/jquery-fundamental-exercise-49.php