Bug with .animate()'s callback?

隐身守侯 提交于 2019-12-13 02:22:06

问题


I'm trying to make some div to animate, then in the callback, call another function. Nothing hard. Here's the code:

function transfertMenu(){
    var chosenOption = this.firstChild.firstChild.id;
        $('#leftMenucontent').animate({
            left:'0px'
        }, 500, constructMenu());
    $('#leftMenucontent > ul > li').remove();
    chooseMenu(chosenOption);
}

and tried

function transfertMenu(){
    var chosenOption = this.firstChild.firstChild.id;
        $('#leftMenucontent').animate({
            left:'0px'
        }, 500, function() {
        constructMenu();
    });
    $('#leftMenucontent > ul > li').remove();
    chooseMenu(chosenOption);
}

The problem is that when I put an alert() in the callback (constructMenu) it works perfectly, the animation finishes, then the alerts pops up. When I enter a function, it starts at the beginning, before the animation completes. Is it a bug, or i'm doing something wrong?


回答1:


You should remove () from callback function because otherwise it gets called immediately:

 $('#leftMenucontent').animate({
    left:'0px'
 }, 500, constructMenu());
------problem --------^

Should be:

 $('#leftMenucontent').animate({
    left:'0px'
 }, 500, constructMenu);

So specify your callback function as constructMenu instead of constructMenu()



来源:https://stackoverflow.com/questions/4473621/bug-with-animates-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!