Terminate Javascript without error / exception

…衆ロ難τιáo~ 提交于 2019-12-06 13:58:54

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();
}

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!