简体   繁体   English

有关JS嵌套if语句的更好解决方案

[英]better solution about JS nesting if statement

Let's say there are 3 divs in my html. 假设我的html中有3个div。 Id's are: ID是:

  • stepone 第一步
  • steptwo 第二步
  • stepthree 第三步

I want first to load in data which is json from server on stepone div, if the return is '1' then print good on stepone div. 我想首先从stepone div上的服务器上加载json数据,如果返回值为“ 1”,则在stepone div上打印良好。

Then load in data on steptwo div, the data is json as well. 然后在steptwo div上加载数据,数据也是json。 So basically this is the process and the following is my code: 所以基本上这是过程,下面是我的代码:

$(document).ready(function(){
    var loadingone = $.post("stepone.php");
        .done(function(dataone){
        objone = JSON && JSON.parse(dataone) || $.parseJSON(dataone);   //get json return from server
        if (objone.status == "1") {
            $("#stepone").html("good");
            var loadingtwo = $.post("steptwo.php")
                .done(function(datatwo){
                    objtwo = JSON && JSON.parse(datatwo) || $.parseJSON(datatwo);
                    if (objtwo.status == "1"){
                        $("#steptwo").html("good");
                    }
                    else
                    {
                        $("#steptwo").html("bad");
                    }
                });
        }
        else
        {
            $("#stepone").html("message" + objone.codeurl);
        }
    });
});

So as you can see the code contains nested if statement and it doesn't look very clear. 因此,如您所见,代码包含嵌套的if语句,看起来不太清楚。 I am wondering if there is a better solution for this? 我想知道是否有更好的解决方案? thx ===updata=== here's the code after i edited, is it right? thx === updata ===这是我编辑后的代码,对吗?

var loadingone = $.post("stepone.php");
    .done(function(dataone){
    objone = JSON && JSON.parse(dataone) || $.parseJSON(dataone);

    if (objone.status != "1"){
        $("#stepone").html("bad" + objone.codeurl")
    }
    else{
        $("#stepone").html("good");
        var loadingtwo = $.post("steptwo.php");
            .done(function(datatwo){
                objtwo = JSON && JSON.parse(datatwo) || $.parseJSON(datatwo);
                if (objtwo.status != "1"){
                    $("#steptwo").html("bad");
                }
                else
                {
                    $("#steptwo").html("good");
                }
            }
    }

Negate your expressions. 否定您的表情。

Rather than following a pattern like: 而不是遵循以下模式:

if A {
    if B {
        if C {
            do stuff
        }
        else error C
    }
    else error B
}
else error A

Try this: 尝试这个:

if !A {
    error A
    return
}
if !B {
    error B
    return
}
if !C {
    error C
    return
}
do stuff

This way, you keep the errors with their respective conditions, and you avoid deep nesting. 这样,您可以将错误与各自的条件保持一致,并避免深层嵌套。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比switch语句更好的解决方案 - Better solution than a switch statement 是否有比使用if / then语句更好的解决方案? - Is there a better solution than using an if/then statement for this? 从 filter 和 findIndex 循环的更好解决方案? 更短的解决方案? JS - Better solution for looping from filter and findIndex? Shorter solution ? JS 需要帮助,用JS / jQuery编写更好的if语句 - Need help writing a better if statement with JS/jQuery JS:制作一个更好的函数以返回条件语句 - JS: Making a better function that returns conditional statement 关于嵌套setInterval - About nesting setInterval 如何在JS中以更好的方式有条件地更改对象属性值? - How to conditionally change object property value in the better way then this solution in JS? 为 Discord.js + MongoDB 打开和关闭事件的更好解决方案? - Better Solution for turning events ON and OFF for Discord.js + MongoDB? 将GraphQL数据拉入gatsby-browser.js(或更好的解决方案) - Pull GraphQL data into gatsby-browser.js (or better solution, please) p5js 中的这个 2D noise() 椭圆有更好的解决方案吗? - Is there a better solution to this 2D noise() ellipse in p5js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM