peace_love
peace_love

Reputation: 6471

How can I output all running jQuery code in console log?

I want to debug my js-script.

It is very long and I want to find out if anywhere is something happening in my script, because it is slowly.

When I was reading through tutorials I could find out that for every function I can write at the end

console.log('Something is happening');

But in my very big document-ready function there are maybe more little executions that I do not know. How can I detect them? Is there a way to just print out "everything" that is happening instead of going through all the code and put console.log's anywhere?

Upvotes: 0

Views: 1399

Answers (3)

errand
errand

Reputation: 980

console.log can be included anywhere in your javascript. every console.log message generates a new line in your log. it would be a lot of work, and you basically don't want to include a log message after every single line of your code.

first you would start to add a console log at the beginning and end of every function, you wrote by yourself. you can also do this on bigger code blocks within a function (e.g. your main function)

the message content should be something like 'start of ' and 'end of '.

if you call jquery functions or have included plugins, check if they offer a "before" and "after" or "start" and "end" callback function. then include your console.log there. be sure, that the written text is unique, so you find your loophole.

with that you would be able to see the sequence in which your functions get executed. you also should be able to see if a function starts but never ends -> this would be the one, where you want to start to get more detailed logs. and of course you should be able to see if anything gets called several times.

Upvotes: 1

Radhesh Vayeda
Radhesh Vayeda

Reputation: 911

Printing everything in console.log() is not a good practice for being a good programmer and it will make task difficult to find out the real bug in your code. Instead use frontend debugging tools like Firebug or you can also use Firefox Developer Edition. You can easily put debug points in your jQuery scripts, observe variables, etc.

Upvotes: 3

Alex Warren
Alex Warren

Reputation: 7547

Instead of using console.log everywhere, use a proper Debugger and attach breakpoints. That will allow you to step through your JavaScript to see exactly what is happening.

This article Pause Your Code With Breakpoints tells you how to set them up in Chrome.

Upvotes: 1

Related Questions