简体   繁体   中英

Passing function parameters on to fadeOut callback

This function accepts up to three parameters. I want to be able to pass those parameters on to the callback function so that I am able to check the arguments there in my if statement.

I checked with console log if there are any parameters present in the callback, and there weren't any.

function foobar(x, y, z) {
    $("div").fadeOut(300, function() {
        if(arguments.length == 1) {
            //nothing
        } else if(arguments.length == 2) {
            $("div").html(x + y).fadeIn(300);
        } else if(arguments.length == 3) {
            $("div").html(x + y + z).fadeIn(300);
        }
    });
}

arguments refers to the nearest function, so here it is a fadeOut callback. Assign foobar function's arguments to the new variable:

function foobar(x, y, z) {
    var foobarArguments = arguments;

    $("div").fadeOut(300, function() {
        if(foobarArguments.length == 1) {
            //nothing
        } else if(foobarArguments.length == 2) {
            $("div").html(x + y).fadeIn(300);
        } else if(foobarArguments.length == 3) {
            $("div").html(x + y + z).fadeIn(300);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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