Checking conditions before allowing a play button to become visible I have this code to check a couple conditions before allowing a play button to become visible. Is there anything to do to consolidate this code?

var isNodeWebkit = (typeof process == "object"); // check if using node app
var downloadURL = 'https://www.microsoft.com/getsilverlight/get-started/install/'; // setup download URL variable
var isEncoding = false; // setup isEncoding variable

if (!Silverlight.isInstalled("1.0")) { // check if silverlight is installed
    $('.playbtn').hide();
    $('.videoposter').append('&lt;button class="installbtn" /&gt;');
    if (isNodeWebkit) {
        $('.installbtn').on('click', function () { // open silverlight download externally
            require('nw.gui').Shell.openExternal(downloadURL);
            var win = require('nw.gui').Window.get();
            win.close(); return false;
        });
    } else {
        $('.installbtn').click(function () { // open silverlight download in new tab
            window.open(downloadURL, '_blank');
        });
        $(window).focus(function () {
            if (Silverlight.isInstalled("1.0")) {
                $('.installbtn').remove();
                $('.playbtn').show(); checkEncodingFunc();
            } else if (!Silverlight.isInstalled("1.0")) {
                $('.installbtn').remove(); $('.videoposter').append('&lt;button class="installbtn" /&gt;');
                $('.playbtn').hide(); $('.encodingbtn').hide();
            }; ;
        });
    };
} else if (Silverlight.isInstalled("1.0")) {
    $(window).focus(function () { // after installing show play button
        if (!Silverlight.isInstalled("1.0")) {
            $('.playbtn').hide(); $('.installbtn').remove(); $('.encodingbtn').remove();
            $('.videoposter').append('&lt;button class="installbtn" /&gt;');
            $('.installbtn').click(function () { // open silverlight download in new tab
                window.open(downloadURL, '_blank');
            });
        } else {
            $('.installbtn').remove();
            $('.playbtn').show();
        };
        if (Silverlight.isInstalled("1.0") &amp;&amp; isEncoding == true) { checkEncodingFunc(); };
    });
};

var encodeTimer = setInterval(function () { if (isEncoding == true) { checkEncodingFunc(); }; }, 60000);
if (Silverlight.isInstalled("1.0")) { checkEncodingFunc(); };

function checkEncodingFunc() { // check if video is encoding
    $.ajax({
        url: 'video/',
        statusCode: {
            200: function () { // if video is found display play button
                if (isEncoding == true) {
                    $('.encodingbtn').remove();
                    $('.playbtn').show();
                    isEncoding = false;
                };
            },
            204: function () { // if video is encoding display encoding button
                if (Silverlight.isInstalled("1.0")) {
                    isEncoding = true;
                    $('.playbtn').hide(); $('.encodingbtn').remove();
                    $('.videoposter').append('&lt;button class="encodingbtn" /&gt;');
                };
            }
        }
    });
};
Just a few more minor things on top of jt0dd's excellent review.



Anonymous functions in JavaScript are awesome, but your program will be more readable if you give the main functions names and declare in advance. That way the main part of your program will become something like this:

if (!haveSilverlight) {
    $('.playbtn').hide();
    $('.videoposter').append('&lt;button class="installbtn" /&gt;');
    if (isNodeWebkit) {
        $('.installbtn').on('click', openExternalSilverlightDownloader);
    } else {
        $('.installbtn').on('click', openSilverlightDownloader);
        $(window).on('focus', setupInstallBtn);
    }
} else {
    $(window).on('focus', setupPlayBtn);
}


Easier to see the overall logic, no?



Don't write if statements this way:


if (something) {
     // ...
} else if (!something) {
     // ...
}



That's what else is for:

if (something) {
     // ...
} else {
     // ...
}




You don't need to evaluate boolean variables like this:


if (isEncoding == true) {
    // ...
}



You can do simply:

if (isEncoding) {
    // ...
}




On this line, you don't need the brackets, and I think the code would be more readable with the comment on the line before the assignment.


var isNodeWebkit = (typeof process == "object"); // check if using node app



Better this way:

// check if using node app
var isNodeWebkit = typeof process == "object";




Especially with long lines like this, definitely don't put comment after the statement, as it forces me to scroll right, which is not convenient. Even better, the a comment saying // setup download URL variable for a variable assignment is pointless, so it's better to just drop it.


var downloadURL = 'https://www.microsoft.com/getsilverlight/get-started/install/'; // setup download URL variable
var isEncoding = false; // setup isEncoding variable



These lines should have been just:

var downloadURL = 'https://www.microsoft.com/getsilverlight/get-started/install/';
var isEncoding = false;


Without comments, assignments are self-explanatory.