Search code examples
javascriptparametersnestedargumentsparent

Passing a function as argument of another function to nested function


Little confusing. From within function evenCard I'd like to call function autoFlip and pass it function evenBack as an argument. Then to complicate things further, autoFlip needs to pass the argument to another function nested within it and then call function evenBack. Is this possible? Thanks!

function evenCard() {
    autoFlip_toggle = true;     
    autoFlip(evenBack);
}

function autoFlip(back) {
    // do other stuff
    $(window).scroll(function() {
         if (autoFlip_toggle == true) {
           // Call evenBack somehow?
         }
     });
}

function evenBack() {
    // Does stuff
}

Solution

  • Call back(); within autoFlip.

    function autoFlip(back) {
        // do other stuff
        $(window).scroll(function() {
             if (autoFlip_toggle == true) {
               back();
             }
         });
    }