How do I find out how many times a function is called with javascript/jquery?

后端 未结 6 625
心在旅途
心在旅途 2020-12-09 04:32

Perhaps an odd question but here it goes: I have a function which I call periodically and within that function I need to know which iteration I\'m in, or how many times the

6条回答
  •  一个人的身影
    2020-12-09 05:07

    You could simply use a global variable, which is increased each time you call the function:

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

    As soon as your code gets a little more complex (or if you use a lot of other libraries), you should, however, consider using scoping as shown in the other answers here (best explained in the one by Vilx).

提交回复
热议问题