Declare a global variable named count
and assign it 0
window.count = 0;
Now, inside the doSomethingElse()
function, increment it like count++
So, whenever you access count
variable, it will return the number of times the doSomethingElse()
is called.
Full code might be:
window.count = 0;
function doSomething(n) {
for (var i = 0; i < n; i++) {
doSomethingElse();
}
}
function doSomethingElse() {
count++;
// do something here
}
doSomething(22);
alert(count);// alerts 22
Or even better, call count++
whenever the function you want to be tested is called in code.
Demo: http://jsfiddle.net/583ZJ/
Note: If you want to remove it, then just remove the variable declaration (window.count=0;
) and count++