How to find out how many times a function is called using javascript / jquery?

Perhaps a strange question, but here it goes: I have a function that I call periodically and inside this function I need to know what iteration I enter, or how many times the function was called. A simplified version of the problem:

jQuery( document ).ready( function(){ setInterval( "myFunction()", 3000 ); }); function myFunction() { alert( "I have been called X times" ); } 

So how can I define X in the above code?

+7
source share
6 answers

You can simply use a global variable that increments every time you call a function:

 var myFuncCalls = 0; function myFunction() { myFuncCalls++; alert( "I have been called " + myFuncCalls + " times" ); } 

Once your code gets a little more complicated (or if you use many other libraries), you should, however, consider using scope as shown in the other answers here (best explained in one on Vilx ).

+7
source

Simple version: make a global variable, as in nyarlathotep answer . The problem is if any other code also defines a global variable with the same name, you are both in trouble.

A simple extended version is to give the variable a crazy name that no one will ever use: calledTimesED7E69A7B141457CA8908A612E3D7A3A

Smart version: add this variable to an existing global variable. Remember - the whole object in Javascript!

 $(function(){ setInterval(myFunction, 3000); }); function myFunction() { myFunction.calledTimes++; alert( "I have been called " + myFunction.calledTimes + " times" ); } myFunction.calledTimes = 0; 

Traditional version: use scope to hide this variable.

 $(function() { calledTimes = 0; setInterval(function() { calledTimes++; alert( "I have been called " + calledTimes + " times" ); }, 3000); }); 

This hides "myFunction", so let's try again with a tricky look:

 var myFunction = null; (function() { calledTimes = 0; myFunction = function() { calledTimes++; alert( "I have been called " + calledTimes + " times" ); } })(); $(function () { setInterval(myFunction, 3000); }); 

... and there are two million more ways to hide this variable with scope. Just choose your favorite.

+18
source

You will need to use closure. Usually you should use a static variable. in Javascript, it will look something like this:

 jQuery( document ).ready( function(){ setInterval( myFunction, 3000 ); }); var myFunction = (function(){ var count = 0; return function(){ count++ alert( "I have been called " + count + " times"); } })(); 

Demo: http://jsfiddle.net/MZQ83/2/

+7
source

Here is another interesting solution that does not use an external variable. The best part of this is that you can leave any pre-existing functions intact and name them as you normally would. This means that if you try to "connect" to a function in an existing library, this will work just fine for you. It adds an unobtrusive counter and allows you to continue to make normal calls to existing functions; even with arguments!

 // no js library required // pre-existing function var a = function(){ console.log("pre-existing function function"); console.log("arguments:", arguments); }; // add counter func var addFnCounter = function(target){ var swap = target; var count = 0; return function(){ swap.apply(null, arguments); count++; console.log("func has been called " + count + " times"); console.log("\n"); }; }; // usage a = addFnCounter(a); // call a() as you would normally a(); a(1,2,3); a('hello', 'world'); // using your setInterval example setInterval(a, 3000); 

Exit

 pre-existing function function arguments: [] func has been called 1 times pre-existing function function arguments: [1, 2, 3] func has been called 2 times pre-existing function function arguments: ["hello", "world"] func has been called 3 times 

setInterval output

 pre-existing function function arguments: [] func has been called 4 times pre-existing function function arguments: [] func has been called 5 times pre-existing function function arguments: [] func has been called 6 times 

See how it works on jsfiddle

+7
source

Create a global variable and initialize it with zero. then increment by one when calling the myfunction () function. Display this variable instead of X.

0
source

The static variable is clean and it will not pollute your external scale, compared to a closure or decorator, as in other answers.

 var foo = function(){ alert( ++foo.count || (foo.count = 1) ); } // test function callTwice(f){ f(); f(); } callTwice(foo) // will alert 1 then 2 

or

 callTwice( function bar(){ alert( ++bar.count || (bar.count = 1) ); }); // will alert 1 then 2 

the second is an anonymous function. And pay attention to this syntax:

 var foo = function bar(){ /* foo === bar in here */ } 
0
source

All Articles