0

这是我的代码。我要做的是使用 jQuery 在我的页面上更新新闻。该代码按预期工作 - 每 5 秒从侧边栏中删除新闻并在其位置发布 2 个更新。我不能做的是让它在发布更新之前删除所有新闻。现在它会追加更新并同时开始淡出旧新闻。任何人都知道如何为此设置适当的超时?

function updateNews()
{

    $('.newsUpdate').load('news.html .news', function ()
    {
        $('.MainNews').fadeOut(); /* timeout should happen here! */
        var start = Math.floor(Math.random() * 4);
        var stop = start + 2;
        $(this).children('.news').slice(start, stop).css('display', 'none').attr('class', 'MainNews').insertAfter('.nav').fadeIn().children('span').remove();
        $(this).children('.news').remove();
    });
    setTimeout(updateNews, 5000);
}
updateNews();

HTML 非常简单:

<div class='sidebar'>
    <ul class='nav'>
        <li></li>
    </ul>
</div>
<article>main content of the site</article>
<div class='newsUpdate'>this created specially to upload news from news.html and manipulate them</div>
4

1 回答 1

1

使用淡出回调

function updateNews()
{

    $('.newsUpdate').load('news.html .news', function ()
    {
        $('.MainNews').fadeOut(1000,function(){
            //This code will be executed after the fadeOut
            var start = Math.floor(Math.random() * 4);
            var stop = start + 2;
            $(this).children('.news').slice(start, stop).css('display', 'none').attr('class', 'MainNews').insertAfter('.nav').fadeIn().children('span').remove();
            $(this).children('.news').remove();
        });
    });
    setTimeout(updateNews, 5000);
}

updateNews();
于 2013-02-24T10:03:56.447 回答