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

을 사용합니다. – marko

+0

중복을 확인하려고합니까? '.MainNews'에 직접'$ .load()'를 사용하지 않는다면 같은 결과를 얻을 수 있습니까? – sjdaws

답변

1

는 당신이 달성 하려는지 명확하게 될 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(); 
+0

나는이 접근 방식을 피곤해서 어떤 이유로 작동하지 않습니다. 이 작업을 수행하면 처음 2 개 뉴스까지 사이드 바에 게시되지 않습니다. –

+0

디버거에 중단 점을 추가하려고 시도 했습니까? F12 – sdespont