+ 2
How do I stop JavaScript code from running?
So I have conditions. If blablabla..... else (and then the code should stop)
3 Respuestas
+ 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.
+ 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.
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.