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.

chouxe
  • 9
  • 3
  • Does this cover what you want? You could check if debug was true before running the solutions listed here. https://stackoverflow.com/questions/5033836/adding-console-log-to-every-function-automatically – Oliver Trampleasure Jan 05 '21 at 11:06
  • 1
    Why do you want to do something like this? Why don't you use the built-in debugger/profiler? – Andreas Jan 05 '21 at 11:09
  • Does this answer your question? [How to get Javascript Function Calls/Trace at Runtime](https://stackoverflow.com/questions/11853256/how-to-get-javascript-function-calls-trace-at-runtime) – freedomn-m Jan 05 '21 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. – chouxe Jan 05 '21 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 – Andreas Jan 05 '21 at 12:46

0 Answers0