Is it possible to stop JavaScript execution? [duplicate]

Is it possible to stop JavaScript execution? [duplicate]

This question already has an answer here:

How to terminate the script in JavaScript?

16 answers

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.

Solutions/Answers:

Solution 1:

Short answer:

throw new Error("Something went badly wrong!");

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.

Related:  JavaScript get child element

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:

// creates a new exception type:
function FatalError(){ Error.apply(this, arguments); this.name = "FatalError"; }
FatalError.prototype = Object.create(Error.prototype);

// and then, use this to trigger the error:
throw new FatalError("Something went badly wrong!");

be sure you don’t have catch() blocks that catch any exception; in this case modify them to rethrow your "FatalError" exception:

catch(exc){ if(exc instanceof FatalError) throw exc; else /* current code here */ }

When a task completes or an arbitrary event happens?

return; will terminate the current function’s execution flow.

if(someEventHappened) return; // Will prevent subsequent code from being executed
alert("This alert will never be shown.");

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 clearTimeout and clearInterval. Finally, to stop XHR (Ajax) requests, you can use the xhrObj.abort() method (which is available in jQuery as well).

Related:  Form Submission without page refresh [duplicate]

Solution 2:

You can make a JavaScript typo 😀 (thinking outside the box here)

thisFunctionDoesNotExistAndWasCreatedWithTheOnlyPurposeOfStopJavascriptExecutionOfAllTypesIncludingCatchAndAnyArbitraryWeirdScenario();

Or something like:

new new

Solution 3:

Something like this might work:

function javascript_abort()
{
   throw new Error('This is not an error. This is just to abort javascript');
}

Taken from here:

http://vikku.info/codesnippets/javascript/forcing-javascript-to-abort-stop-javascript-execution-at-any-time/

Solution 4:

No.

Even if you throw an exception, it will only kill the current event loop. Callbacks passed to setTimeout or DOM/XMLHttpRequest event handlers will still run when their time comes.

Solution 5:

I do:

setTimeout(function() { debugger; }, 5000)

this way I have 5 seconds to interact with UI and then in stops. Las time I used was when I needed to leave custom tooltip visible, to do some styling changes.

Solution 6:

I am using

return false;

if I want to abort from JavaScript from running further downwards.