jQuery函数内部超时
这是我的密码。我试图做的是使用jQuery更新我页面上的新闻。代码按预期工作-每5秒从侧边栏中删除新闻,并在其位置发布2条更新。我不能做的是让它在发布更新之前删除所有新闻。目前,它在添加更新的同时开始淡出旧新闻。 有人知道如何为此设置适当的超时吗jQuery函数内部超时,jquery,timeout,Jquery,Timeout,这是我的密码。我试图做的是使用jQuery更新我页面上的新闻。代码按预期工作-每5秒从侧边栏中删除新闻,并在其位置发布2条更新。我不能做的是让它在发布更新之前删除所有新闻。目前,它在添加更新的同时开始淡出旧新闻。 有人知道如何为此设置适当的超时吗 function updateNews() { $('.newsUpdate').load('news.html .news', function () { $('.MainNews').fadeOut(); /* t
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>
网站的主要内容
这是专门为从news.html上传新闻并对其进行操作而创建的
使用淡出回调
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();
显示html,这样就可以更清楚地了解您要完成的任务。您是否要检查重复项?如果不是,使用
$.load()
直接进入.MainNews
会不会达到同样的效果?我厌倦了这种方法,因为某种原因它不起作用。当我这样做的时候,边栏上没有任何东西被发布,即使是前2条新闻。