2013-02-24 33 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