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.
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.
Short answer:
throw new Error("Something went badly wrong!");
If you want to know more, keep reading.
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.
Instead of trying to stop everything, let your code handle the error. Read about Exception
s 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:
// 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 */ }
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.
...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).
setTimeout
or XMLHttpRequest
will still execute. Currently there is no standard way to completely terminate JavaScript.
Jul 17, 2013 at 22:01
debugger;
. This will halt absolutely everything from executing.
Jul 17, 2013 at 22:03
debugger;
statement. return
outside functions produces syntax error (just to make clear for surfers). There are so many aspects to consider... I was just successful by embracing the part I didn't want to be executed with if (false) { ... }
. So you don't have to care about nested comments.
Aug 18, 2014 at 12:53
debugger
is great for halting a page from loading when there is lots of asynchronous stuff going on, but it would be nice if one could get rid of the translucent gray overlay and scroll in the page at the same time.
You can make a JavaScript typo :D (thinking outside the box here)
thisFunctionDoesNotExistAndWasCreatedWithTheOnlyPurposeOfStopJavascriptExecutionOfAllTypesIncludingCatchAndAnyArbitraryWeirdScenario();
Or something like:
new new
abcd
helped: script compiles but falls here with 'not defined' runtime error! Thanks a lot, this should be the answer.
fakeFunctionThatEffectivelyStopsAllProcessingInJavaScript();
. Thanks.
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:
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.
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.
I am using
return false;
if I want to abort from JavaScript from running further downwards.
If you're in a function you can exit it using return;
but that doesn't stop execution of the parent function that called that function.
You can call return
early in a function, and at least that function will stop running. You can also just use throw ''
to cause an error and stop the current process. But these won't stop everything. setTimeout
and setInterval
can make delayed functions and functions that run on a time interval, respectively. Those will continue to run. Javascript events will also continue to work as usual.
I know this is old, but I wanted to do this and I have found, in my opinion, a slightly improved solution of the throw answers. Just temporary supress the error messages and reactivate them later using setTimeout :
setTimeout(function() {
window.onerror = function(message, url, lineNumber) {
return false;
};
}, 50); // sets a slight delay and then restores normal error reporting
window.onerror = function(message, url, lineNumber) {
return true;
};
throw new Error('controlledError');
Define a variable inside the JavaScript function, set this variable to 1 if you want ot execute the function and set it to 0 if you want to stop it
var execute;
function do_something()
{
if (execute == 1)
{
// execute your function
}
else
{
// do nothing
}
}
The process is tedious, but in Firefox:
stop or terminate JavaScript this [in a] way to [that it] prevent[s] any further JavaScript-based execution from occuring, without reloading the browser
Notes:
Other browsers have and use different conventions.
debugger;
to freeze all javascript loops of a page. Very useful to kill annoying javascript, especially when the page requires javascript to load.debugger
is great for halting a page from loading when there is lots of asynchronous stuff going on, but it would be nice if one could get rid of the translucent gray overlay and scroll/use the page at the same time. Anyone know how to do that?