这是我的代码。我想做的是使用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>
使用fadeOut回调
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();