6

I need to terminate the javascript from a function so that it doesn't execute the next block of codes and other functions called by the script, which follows the current function.

I found this existing question in SO : How to terminate the script in Javascript , where the solution is given as to trigger an error / exception which will terminate the script immediately.

But my case is different because, My purpose of terminating the script is to prevent an error which will come from the next functions, if the current function doesn't stop execution of the script. So, triggering another error to prevent an error is no way solving my purpose!

So, is there any other way of terminating the javascript instead of throwing an error / exception?

Please read my question properly before marking as duplicate, I have already explained why it doesn't solve my problem with the question I referred!.

9
  • Possible duplicate of How to terminate the script in Javascript Commented Jan 12, 2016 at 4:17
  • 2
    Your requirement is unreasonable in the browser environment. You'll need to rearrange your logic so that you can just not call the functions that'll error out. Or set a flag that they check and return early if it's set or something. Commented Jan 12, 2016 at 4:21
  • I read it completely, Tareq. Didn't you read the suggestions here and all of the answers there? Create a control structure in a main function and return to it from your function. You then have the power to decide if you will continue operations or gracefully allow the script to terminate. Commented Jan 12, 2016 at 4:28
  • Is using jQuery an option ? Commented Jan 12, 2016 at 4:28
  • @guest271314, Sure, if possible without error. Commented Jan 12, 2016 at 4:28

2 Answers 2

5

Since you're inside a function, just return.

Reading into your question a little more that it doesn't execute "other functions called by the script", you should return a boolean. For example, return false if you don't want the rest called, then wrap the remaining statements in an if:

function Foo() {
    if (weAreInLotsOfTrouble) {
        return false;
    }

    return true;
}

DoSomething();
DoSomethingElse();

if (Foo()) {
    DoSomethingDangerous();
}
Sign up to request clarification or add additional context in comments.

3 Comments

That will only terminate the function, not the script, I need to stop the functions following the current function to stop executing!
So return a true/false from the script and have your main function decide whether or not to proceed based on the return value.
@DavidHoelzer, Yeah, that's a possible solution. We can do that way in PHP too, but still we have a function called exit(), which reduces our task. I just wanted to know, if there is any way to kill the script without error in Javascript, if not, we can find other solutions anyway.
0

Try using jQuery.Callbacks() with parameter "stopOnFalse" ; jQuery.when() , jQuery .then()

var callbacks = $.Callbacks("stopOnFalse");

function a(msg) {
  var msg = msg || "a";
  console.log(msg);
  return msg
}
// stop callbacks here
function b() {
  var msg = false;
  console.log(msg)
  return msg
}
// `c` should not be called
function c() {
  var msg = new Error("def");
  console.log(msg)
  return msg
}

callbacks.add(a,b,c);

$.when(callbacks.fire())
.then(function(cb) {
  // `c` : `Error`
  if (cb.has(c)) {
    console.log("callbacks has `c`"
                , cb.has(c));
    // remove `c`
    cb.remove(c);
    console.log(cb.has(c));
    // empty `callbacks`
    cb.empty();
    // do other stuff
    cb.add(a);
    cb.fire("complete")
    
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

3 Comments

OMG, this looks more complex than the solution provided by the other answer. Anyways thanks for trying to help, but I was only looking for simple solution for stopping script, not callbacks.
Use solution posted by @lc
Yeah, that's what I was talking about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.