开发者

Set jQuery code to fire after function has completed

I have the following function (I've removed code from the middle that is not important to my question):

function shadowBoxRefresh(){
    $("#sb-nav-next, #sb-nav-previous").click(function(){
            $('#sb-container').addClass("visibility_stay");
            $('#sb-overlay').addClass("opacity_stay");
            Shadowbox.close();
            Shadowbox.clearCache();
            shadowBoxSetup();
            setTimeout("Shadowbox.open(c)", 400)
            $('#sb-container').removeClass("visibility_stay");
            $('#sb-overlay').removeClass("opacity_stay");
        }
    });
}

My problem is, I need this part:

$('#sb-container').removeClass("visibility_stay");
$('#sb-overlay').removeClass("opacity_stay");

to fire after the rest of the function has completed. I'm wondering if a callback would do the job, 开发者_Python百科but I'm not versed well enough in callbacks to know how to implement it.

Your help would be much appreciated.


If by "after the rest of the function" you mean, "after the Shadowbox.open(c)" that happens .4 sec later, then do this:

function shadowBoxRefresh(){
    $("#sb-nav-next, #sb-nav-previous").click(function(){
            $('#sb-container').addClass("visibility_stay");
            $('#sb-overlay').addClass("opacity_stay");
            Shadowbox.close();
            Shadowbox.clearCache();
            shadowBoxSetup();
            setTimeout(function () {
                Shadowbox.open(c);
                $('#sb-container').removeClass("visibility_stay");
                $('#sb-overlay').removeClass("opacity_stay");
            }, 400);
        }
    });
}


Assuming after shadowBoxSetup() is when you want it

function shadowBoxSetup( callback ) {
    // Your code...
    callback();
}

To use that

function shadowBoxRefresh(){
    $("#sb-nav-next, #sb-nav-previous").click(function(){
            $('#sb-container').addClass("visibility_stay");
            $('#sb-overlay').addClass("opacity_stay");
            Shadowbox.close();
            Shadowbox.clearCache();
            shadowBoxSetup(function(){
                $('#sb-container').removeClass("visibility_stay");
                $('#sb-overlay').removeClass("opacity_stay");
            });
            setTimeout("Shadowbox.open(c)", 400);
        }
    });
}


It will be executed after the rest of the function has completed; statements are executed in order.

Are you saying you want it to execute after the timeout? If so, create a function that encapsulates the two calls and the open() call.

If not, you might need to be a bit clearer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜