Javascript函数延迟

Javascript函数延迟,javascript,jquery,function,timer,Javascript,Jquery,Function,Timer,我做了一个小javascript。像这样: content.css('height', contentHeight, function() { alert("test"); $("body").scrollTo("#main", 600); }); 我希望在设置内容高度后执行警报。但是我做错了什么?函数是瞬时的 content.css('height', contentHeight); alert("test"); $("body").scrollTo("#main", 60

我做了一个小javascript。像这样:

content.css('height', contentHeight, function() {
    alert("test");
     $("body").scrollTo("#main", 600);
});
我希望在设置内容高度后执行警报。但是我做错了什么?

函数是瞬时的

content.css('height', contentHeight);
alert("test");
$("body").scrollTo("#main", 600);
css()
这样的方法没有“完成”处理程序。它立即执行

因此,您必须按如下方式编写代码:

content.css("height", contentHeight);
alert("test");
$("body").scrollTo("#main", 600);
但是,如果在设置高度后需要增加一些延迟,可以使用
setTimeout()


jquery css方法中的函数表示不同:

content.css('height', function(){
    return contentHeight;
});
内容是一个数组,上面的代码为该数组中的所有元素设置高度。此功能的用法可能如下所示:

content.css('height', function(index){
    return index * contentHeight;
});

在这种情况下,下一个元素的高度将增加contentHeight值。

使用animate,完成时调用回调

content.animate({
    height: contentHeight
}, 0, function() {
    alert("test");
    $("body").scrollTo("#main", 600);
});
试用

content.animate(
    {height : 100}, 
    5000,
    function() {
        //Animation complete.
        alert("test");
        $("body").scrollTo("#main", 600);
    }
);
content.animate(
    {height : 100}, 
    5000,
    function() {
        //Animation complete.
        alert("test");
        $("body").scrollTo("#main", 600);
    }
);