简体   繁体   English

我可以将这些嵌套的JavaScript“ if”语句简化为一个吗?

[英]Can I simplify these nested javascript “if” statements into one?

So I have two variables, animationFlag and ajaxFlag. 所以我有两个变量,animationFlag和ajaxFlag。 I also have a function setup that returns true or false based on browser support of transitions. 我也有一个功能设置,它根据浏览器对转换的支持返回true或false。 That I'd like to happen is that it only runs this fadein function if ajaxFlag is true AND animationFlag is true. 我想发生的事情是,只有在ajaxFlag为true且animationFlag为true时,它才会运行此fadein函数。 But in a browser without transition support, animationFlag will never become true so I need to check if it supports transitions, then check for the ajaxFlag only if transitions aren't supported. 但是在没有过渡支持的浏览器中,animationFlag永远不会变为true,因此我需要检查它是否支持过渡,然后仅在不支持过渡的情况下检查ajaxFlag。 I tried separating them with || 我试图用||将它们分开 and && in different combinations, but I really need this nesting. 和&&的不同组合,但我确实需要此嵌套。

A single if statement would be WAYYYY nice, so, is it possible to combine that into one? 一个if语句将是WAYYYY不错的,那么,可以将其组合为一个吗? Thanks! 谢谢!

    function checkDone() {
        if (!supportsTransitions) {
            if (ajaxFlag) {
                setTimeout(function(){
                    fadeInModal();
                },100)
            }
        } else {
            if (ajaxFlag && animationFlag) {
                setTimeout(function(){
                    fadeInModal();
                },100)
            }
        }
    }

This should work 这应该工作

if (ajaxFlag && (animationFlag || !supportsTransitions)) {
    // ...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM