Is it possible in some way to stop or terminate JavaScript in a way that it prevents any further JavaScript-based execution from occuring, without reloading the browser?
I am thinking of a JavaScript equivalent of exit() in PHP.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short answer:
If you want to know more, keep reading.
Do you want to stop JavaScript’s execution for developing/debugging?
The expression
debugger;in your code, will halt the page execution, and then your browser’s developer tools will allow you to review the state of your page at the moment it was frozen.Do you want to stop your application arbitrarily and by design?
On error?
Instead of trying to stop everything, let your code handle the error. Read about
Exceptions by googling. They are a smart way to let your code “jump” to error handling procedures without using tedious if/else blocks.After reading about them, if you believe that interrupting the whole code is absolutely the only option, throwing an exception that is not going to be “caught” anywhere except in your application’s “root” scope is the solution:
be sure you don’t have
catch()blocks that catch any exception; in this case modify them to rethrow your"FatalError"exception:When a task completes or an arbitrary event happens?
return;will terminate the current function’s execution flow.Note:
return;works only within a function.In both cases…
…you may want to know how to stop asynchronous code as well. It’s done with
clearTimeoutandclearInterval. Finally, to stop XHR (Ajax) requests, you can use thexhrObj.abort()method (which is available in jQuery as well).