2013-02-24 67 views
0

这是我的代码。我想要做的是使用jQuery在我的页面更新新闻。该代码按预期工作 - 每隔5秒钟从侧边栏删除新闻并在他们的位置发布2个更新。我不能做的是在发布更新之前删除所有消息。现在它追加更新并同时开始淡出旧消息。 任何人有想法如何设置适当的超时这个?jQuery超时功能内

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> 
+0

显示HTML那就更清楚你要完成的任务。 – marko 2013-02-24 09:06:54

+0

你想检查重复吗?如果不是不会直接在'.MainNews'中使用'$ .load()'实现相同的结果? – sjdaws 2013-02-24 09:35:11

回答

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(); 
+0

我厌倦了这种方法,出于某种原因它不起作用。当我这样做时,即使是前两个消息,也没有任何东西在侧边栏上发布。 – 2013-02-24 17:11:04

+0

您是否尝试在调试器中添加断点? F12 – sdespont 2013-02-24 17:35:04