2012-09-02 46 views
0

我有一个jquery脚本正确加载无限滚动设置的新页面。我想要做的就是让它在距离底部300px的地方说出来,然后加载更多。我现在能够做到这一点,但不幸的是滚动注册多次,阿贾克斯加载页面的其余部分。我试过setTimeout无济于事。延迟无限滚动jQuery或JavaScript

 (function(){ 

      //inner functions will be aware of this 
      var currentPage = 1; 

       $(window).scroll(function() { 

       if($(window).scrollTop() + $(window).height() > $(document).height() - 300) { 

        $.ajax({ 
         type: "GET", 
         url: "posts/page/" + currentPage, 
         data: "", 
         success: function(results){ 
          $(".container").append(results).masonry('reload'); 
         } 
        }) 
        setTimeout(currentPage++,3000); 

       } 

      }); 

     })(); 

回答

6

经过这样的修改,你不执行另一个AJAX请求如果以前尚未结束:

(function(){ 

    //inner functions will be aware of this 
    var currentPage = 1, 
     currentXHR; 

     $(window).scroll(function() { 

     if($(window).scrollTop() + $(window).height() > $(document).height() - 300) { 

      if (currentXHR) { 
       return; 
      } 

      currentXHR = $.ajax({ 
       type: "GET", 
       url: "posts/page/" + currentPage++, 
       data: "", 
       success: function(results){ 
        $(".container").append(results).masonry('reload'); 
       }, 
       complete: function() { 
        currentXHR = null; 
       } 
      }) 

     } 

    }); 

})(); 
+0

完美:)只是出于好奇心xhr代表什么? – bswinnerton

+1

@bswinnerton:'XMLHttpRequest' – zerkms

0

有这个here一个极好的RailsCast(不用担心,如果你不是Ruby,你只是得到了JSON数据。)