4

I am struggling with the fact that the flow of control in javascript is not what I'm used to. Not linear. Sometimes when I test I get the error message "A script is slowing down your browser" at times when everything should be stopped.

I spend ages looking for the bug. This time it was something really silly, a variable which stopped the function when it reached 350 px. Then I added a way to change the speed and sometimes this variable was incremented by 3 at each step. So it never reached 350, it went from 348 to 351.

So I was wondering if there was a "STOP!" command. Something which would leave the display of the web page in its current state but stop all running processes?

Alain Reve
  • 163
  • 1
  • 1
  • 11

2 Answers2

6

Debugger? It just creates a breakpoint, where you can analyse what's going on right now. I know it's not exactly what you're asking about, but it could work for your use case.


Usage

debugger;

Can be used from within console too.


Learn more

https://www.w3schools.com/js/js_debugging.asp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger

PiggyPlex
  • 631
  • 1
  • 4
  • 15
  • 3
    I recommend adding a semicolon after the debugger statement because of best practices. In addition, it also helps people recognize that it's a statement. Simply saying `debugger` might not make anything click in someone's head, but adding a semicolon can be the tipping point that makes the last puzzle piece fall in place and makes that person go "a-ha!". Also note that (at least for chrome) debugger will only work when the console is opened. – Reality Apr 01 '21 at 16:47
  • I was about to comment that it only works with the console open. I would really love something that could simply freeze the page in it's current state and stop any further scripts from executing. In the console->preferences->debugger->Disable Javscript checkbox does just what I want except it's a PITA to get to. – Altimus Prime Apr 01 '21 at 17:03
2

Depending on what you want here, the main answer is "no".

There are a few "hacks" (but they are quite intrusive and unreliable).

A good one (like mentioned in another answer here) is to throw the statement debugger; (that's all). Example:

let eggCount = 0;
function makeEggs(eggCount){
  //Stop! The user is eating too much!!!
  if(eggCount > 5){
    debugger;
  }
  else{
    make_egg("Over-hard");
    return eggCount++;
  }
}

However, this only works (or at least for chrome) if the console is open.

The second big one is to throw an error in your script (as shown by this post). It's hacky, and you can't have any try...catch loops (if you do then you have to throw an error, so you'd to re-throw that error).

let eggCount = 0;
function makeEggs(eggCount){
  //Stop! The user is eating too much!!!
  if(eggCount > 5){
    throw "Egg overflow!";
  }
  else{
    make_egg("Sunny side up");
    return eggCount++;
  }
}

The last big thing you can do (as mentioned by a comment here) is to re-write your logic.

From the looks of it, you have something like this:

let eggCount = 0;
function makeEggs(eggCount){
  //Stop! The user is eating too much!!!
  if(eggCount === 5){
    throw "STOP!"
  }
  else{
    make_egg("Over-hard");
    return eggCount++;
  }
}

Well, what if we did this?

eggCount = makeEggs(eggCount);
eggCount = 6;
eggCount = makeEggs(eggCount);

Since the equals sign checks for if it's just five, you can always bypass it.

This isn't a good idea, so we can re-wire our logic to do this instead:

let eggCount = 0;
function makeEggs(eggCount){
  //Stop! The user is eating too much!!!
  if(eggCount >= 5){
    throw "STOP!"
  }
  else{
    make_egg("Over-hard");
  }
}

Now it will stop if it's 5 or greater.

This:

eggCount = makeEggs(eggCount);
eggCount = 6;
eggCount = makeEggs(eggCount);

will still throw the "STOP" error, even though it's greater than 5.

Sometimes, it's as simple as re-writing your logic.

You don't need the big hack-y stuff if it's as simple as just making your program smarter ;)

I've ran into this type of problem before.

I haven't encountered an incident yet where re-writing my logic doesn't help!

Reality
  • 637
  • 1
  • 6
  • 25