+ 2

How do I stop JavaScript code from running?

So I have conditions. If blablabla..... else (and then the code should stop)

5th Mar 2018, 9:12 PM
Paul Grasser
Paul Grasser - avatar
3 Réponses
+ 4
My solution for that condition is to make sure there are no events to wake up the code once more and return out of the code right after executing: document.write("program has stopped."); This replaces all of the DOM and any on event attributes with it so the only other possibility is timer events, in my code at least.
5th Mar 2018, 10:05 PM
John Wells
John Wells - avatar
+ 2
Depends on the code. In something like a loop a break statement would be required. In most other circumstances you would just let your code run until it finishes.
6th Mar 2018, 12:47 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
0
If you're just wondering how to stop execution after the code finishes normally that is handled automatically by the interpreter once it reaches the end. If you're trying to stop executing code when a check you've built in finds an error you can use throw to throw an error and wrap the code in a try/catch block to catch the error and print a message. This way if the code executes without error it will not be stopped, and you don't need to keep any global Boolean or constantly check it to see if the program should stop.
5th Mar 2018, 11:52 PM
Tom Shaver
Tom Shaver - avatar