7

I have a variable in my javascript file that keeps getting changed and I can't figure out why or when it is getting changed.

Is there a way to set a breakpoint or something when the variable gets changed? I'm currently using Safari on a Mac but also have Chrome installed so if there is an easier way in Chrome Developer Tools I can do that as well. Or of course if there is a separate tool that allows for this type of debugging that is awesome as well.

Is this even possible? If so how?

Thanks so much in advance.

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • You could try using a watch expression on that variable. – elclanrs Jun 30 '16 at 18:55
  • @elclanrs For some reason the watch expression isn't actually setting a break point in the code. It's just printing what the current value of that variable. I'm looking to see exactly where that variable gets changed in code. I can't find where it's getting changed. – Charlie Fish Jun 30 '16 at 19:25
  • [duplicate](https://stackoverflow.com/questions/7910623/javascript-debugging-is-there-a-way-to-break-on-a-change-of-variable-value) In Firefox **watch** method, in Chrome, so far only by polyfill, which wasn't working for me on "window" object for watching change of a global variable. I jumped to firefox for that. – papo Sep 03 '17 at 20:43

2 Answers2

-3

If using Chrome, CTR-SHIFT-I(Options/three bars button --> More tools --> Developer Tools) opens a developer interface. If you open any code in the Sources tab, there's a menu to the right with options for multiple types of breaks that you can insert into your code.

Sean Branchaw
  • 597
  • 1
  • 5
  • 21
  • Yep aware of that. Trying to find what line the variable is getting changed on. I have tried the watch option but that doesn't actually set a breakpoint it only displays the current value of the variable. – Charlie Fish Jun 30 '16 at 19:27
  • Could you update your question with some representative code then? With as little specifics as you provided, this is about as much guidance as anyone can give you. – Sean Branchaw Jun 30 '16 at 19:59
-3

I haven't used Mac or Safari but it should still definitely be possible with Chrome's debugger.

Alt-click on the page and choose "Inspect" to bring up Developer Tools. From the tabs, choose "Sources" and make sure your page is selected on the index on the left. Breakpoints and other debugging tools are available there.

  • I'm aware of that. And I know how to set break points on specific lines of code. I'm trying to find what line the variable is getting changed at. I can't find where the variable is getting changed. I don't even know what line it's getting changed on. So normal line break points won't help. – Charlie Fish Jun 30 '16 at 19:26
  • 2
    I didn't realize that, try the javascript suggested from [this question](http://stackoverflow.com/questions/11618278/how-to-break-on-property-change-in-chrome) – Growlin'Grandpas Jun 30 '16 at 20:43