Is it possible to stop JavaScript execution? [duplicate]

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.



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


Or something like:

new new


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 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.


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.


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
 // do nothing

The process is tedious, but in Firefox:

  1. Open a blank tab/window to create a new environment for the script from the current page
  2. Populate that new environment with the script to execute
  3. Activate the script in the new environment
  4. Close (that is, kill) that new environment to ...

stop or terminate JavaScript this [in a] way to [that it] prevent[s] any further JavaScript-based execution from occuring, without reloading the browser


  • Step 4 only stops execution of JavaScript in that environment and not the scripts of any other windows
  • The original page is not reloaded but a new tab/window is loaded with the script
  • When a tab/window is closed, everything in that environment is gone: all remnants, partial results, code, etc.
  • Results must migrate back to the parent or another window for preservation
  • To rerun the code, the above steps must be repeated

Other browsers have and use different conventions.


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.

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); = "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... 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).


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:


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');


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.