Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:if pass else死亡_Javascript_Jquery - Fatal编程技术网

Javascript jQuery:if pass else死亡

Javascript jQuery:if pass else死亡,javascript,jquery,Javascript,Jquery,我想要一个方法来防止代码在条件不正确时恢复他正在做的事情 这是我的密码 function doSomething{ if(1==2){ alert("Can I access to your event?"); }else{ //1 not equals 2, so please die // I tried die() event; It worked, But i get this error in the console

我想要一个方法来防止代码在条件不正确时恢复他正在做的事情

这是我的密码

function doSomething{
    if(1==2){
        alert("Can I access to your event?");
    }else{
        //1 not equals 2, so please die
       // I tried die() event; It worked, But i get this error in the console
      //  Uncaught TypeError: Cannot call method 'die' of undefined 
    }
}

$(".foo").click(function(){
    doSomething();
    alert("welcome, 1 == 1 is true");
}

我想,您可以在click处理程序中返回false。例如:

function doSomething () {
    if(1==2){
      alert("Can I access to your event?");
    }else{
      return false; // tell the click handler that this function barfed
    }
}

$(".foo").click(function(){
    if(doSomething() === false){ //check to see if this function had an error
      return false; //prevent execution of code below this conditional
    }
    alert("welcome, 1 == 1 is true");
}

我想,您可以在click处理程序中返回false。例如:

function doSomething () {
    if(1==2){
      alert("Can I access to your event?");
    }else{
      return false; // tell the click handler that this function barfed
    }
}

$(".foo").click(function(){
    if(doSomething() === false){ //check to see if this function had an error
      return false; //prevent execution of code below this conditional
    }
    alert("welcome, 1 == 1 is true");
}

从代码判断,您可能只想抛出一个异常;-)


这将立即展开堆栈,直到捕获异常或异常达到全局级别。

从代码判断,您可能只想抛出一个异常;-)


这将立即展开堆栈,直到捕获异常或异常达到全局级别。

尝试这种传统方法

function doSomething () {
    if(1==2){
      alert("Can I access to your event?");
      return true;
    }else{
      return false
    }
}
用法:

$(".foo").click(function(){
    if(doSomething()){
      alert("welcome, 1 == 1 is true");
    }else{
     alert("Sorry, 1 == 1 is false");
    }

}试试这种传统的方法

function doSomething () {
    if(1==2){
      alert("Can I access to your event?");
      return true;
    }else{
      return false
    }
}
用法:

$(".foo").click(function(){
    if(doSomething()){
      alert("welcome, 1 == 1 is true");
    }else{
     alert("Sorry, 1 == 1 is false");
    }
}您可以抛出:

当然,您应该处理该异常以避免日志中出现错误,可能是这样的:

$(".foo").click(function () {
    try {
        doSomething();
        alert("welcome, 1 == 1 is true");
    } catch (err) { 
        // do nothing but allow to gracefully continue 
    }
});
您可以抛出:

当然,您应该处理该异常以避免日志中出现错误,可能是这样的:

$(".foo").click(function () {
    try {
        doSomething();
        alert("welcome, 1 == 1 is true");
    } catch (err) { 
        // do nothing but allow to gracefully continue 
    }
});

JavaScript默认情况下没有
die()
函数。您指的是什么?您想防止什么?只有错误才会“杀死”js代码,并且您不希望看到您所注意到的错误。@zsawyer它在1.9中被完全删除@JimmyTodd,使用
.on()
.off()
@JimmyTodd:那么你是在问不推荐/过时的
jQuery.fn.die()
函数的用法吗?JavaScript默认情况下没有
die()
函数。你指的是什么?你想防止什么?只有错误才会“杀死”js代码,并且您不希望看到您所注意到的错误。@zsawyer它在1.9中被完全删除@JimmyTodd,使用
.on()
.off()
@JimmyTodd:那么你是在问不推荐/过时的
jQuery.fn.die()
函数的用法吗?@sksallaj很抱歉没有得到它^ ^哈哈,不,我的意思是我使用了你的解决方案,并将其作为一个工作示例实现了。。仅此而已:-)@sksallaj抱歉没有得到它^lol,noo,我的意思是我使用了您的解决方案,并将其作为一个工作示例来实现。。仅此而已:-)