Skip to content
Advertisement

My displayError() function is not working at all

I made a function that should display an error or remove an error.

Unfortunately, when I use the function in any way, for example like displayError(true, "test");, it’s not working.

When I check my html code to see if anything changes, nothing is changed.

function displayError(display, string, xhr) {
    $("div#error").fadeOut(300, function() {
        if(arguments.length == 1 && typeof display === "boolean" && display == false) {
            //do nothing
        } else if(arguments.length == 2 && typeof display === "boolean" && display == true && typeof string == "string") {
            $("div#error").html('<b style="color: #ce1919;">(!)</b> '+string).fadeIn(300);
        } else if(arguments.length == 3 && typeof display === "boolean" && display == true && typeof string == "string" && typeof xhr === "object") {
            $("div#error").html('<b style="color: #ce1919;">('+xhr.status+")</b> "+string).fadeIn(300);
        }
    });
}

Anybody who can identify the problems?

Advertisement

Answer

I found out about the console log method. So I tried to check if my function was fired at all and it was. But then I found that the arguments array was just empty, which meant that the parameters were not available in the callback. So I stored the arguments in a variable and then used that variable in my if statements in the callback.

Like so:

function displayError(display, string, xhr) {
    var args = arguments;
    $("div#error").fadeOut(300, function() {
        if(args.length == 1 && typeof display === "boolean" && display == false) {
            //do nothing 
        } else if(args.length == 2 && typeof display === "boolean" && display == true && typeof string == "string") {
            $("div#error").html('<b style="color: #ce1919;">(!)</b> '+string).fadeIn(300);
        } else if(args.length == 3 && typeof display === "boolean" && display == true && typeof string == "string" && typeof xhr === "object") {
            $("div#error").html('<b style="color: #ce1919;">('+xhr.status+")</b> "+string).fadeIn(300);
        }
    });
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement