0

I'm trying to know if in JQuery/JS, it's possible to add an global listener when a function or class method called, or event ? this idea is to display the name function / event in the console for knowing the order and witch function / method was called when debug is true.

Exemple, for now i must call the function debug in all function :

var showdebug = true;

function test(){
    debug();
    test2();
}
function test2(){
    debug();
    ...
}
function debug(){
    if(showdebug === true){
     console.log("function name called")
    }
}
test();

That display in console

test
test2

What I'm would like do, is not add debug() in each function, i'm searching to do var showdebug = true;

if(showdebug === true){
     add listener when all function / method called
     and this listener call automatically debug()
}
function test(){
    test2();
}
function test2(){
    ...
}
function debug(){
    console.log("function name called")
}
test();

So when i called test, this listener called auto the debug function for display in console

test
test2

Thanks for you help.

5
  • Does this cover what you want? You could check if debug was true before running the solutions listed here. stackoverflow.com/questions/5033836/… Commented Jan 5, 2021 at 11:06
  • 1
    Why do you want to do something like this? Why don't you use the built-in debugger/profiler? Commented Jan 5, 2021 at 11:09
  • Does this answer your question? How to get Javascript Function Calls/Trace at Runtime Commented Jan 5, 2021 at 11:11
  • thanks, i will check, not found this solutions when searching. @Andreas : because when having lot of functions called, when debug, i can easy know exactly witch functions called, and in witch order. i already have the first solution set, but i'm asking just if it's possible to do it more automatically without add each time the debug function called. Commented Jan 5, 2021 at 11:28
  • "i can easy know exactly witch functions called, and in witch order." - That's what the call stack in the debugger will tell you Commented Jan 5, 2021 at 12:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.