当滚动速度很快时,ajax请求会多次触发

时间:2012-11-01 06:07:30

标签: javascript jquery jquery-plugins

这里我试图实现无限滚动,但是当我滚动得太快时会发生什么,它会用相同的参数激发多个ajax请求,这会再次导致相同的数据n。从这个问题中克服这个问题请帮忙。

(function( $ ){


 $.fn.scrollPagination = function(options) {

        var opts = $.extend($.fn.scrollPagination.defaults, options);  
        var target = opts.scrollTarget;
        if (target == null){
            target = obj; 
        }
        opts.scrollTarget = target;

        return this.each(function() {
          $.fn.scrollPagination.init($(this), opts);
        });

  };

  $.fn.stopScrollPagination = function(){
      return this.each(function() {
        $(this).attr('scrollPagination', 'disabled');
      });

  };

  var itr = 2;
  $.fn.scrollPagination.loadContent = function(obj, opts){
     var target = opts.scrollTarget;
     var mayLoadContent = $(target).scrollTop()+opts.heightOffset >= $(document).height() - $(target).height();
     if (mayLoadContent){
         if (opts.beforeLoad != null){
            opts.beforeLoad(); 
         }

         $(obj).children().attr('rel', 'loaded');
         $.ajax({
              type: 'POST',
              url: opts.contentPage+"?iteration="+itr,
              data: opts.contentData,
              success: function(data){
                itr++;
                $(obj).append(data); 
                var objectsRendered = $(obj).children('[rel!=loaded]');

                if (opts.afterLoad != null){
                    opts.afterLoad(objectsRendered);    
                }
              }
         });
     }

  };

  $.fn.scrollPagination.init = function(obj, opts){
     var target = opts.scrollTarget;
     $(obj).attr('scrollPagination', 'enabled');

     $(target).scroll(function(event){
        if ($(obj).attr('scrollPagination') == 'enabled'){
                $.fn.scrollPagination.loadContent(obj, opts);
                //alert(event.isPropagationStopped());
        }           
            //event.stopPropagation();
            //console.log(event.isPropagationStopped());
            event.preventDefault();
     });

     //$.fn.scrollPagination.loadContent(obj, opts);

 };

 $.fn.scrollPagination.defaults = {
         'contentPage' : null,
         'contentData' : {},
         'beforeLoad': null,
         'afterLoad': null  ,
         'scrollTarget': null,
         'heightOffset': 0        
 }; 
})( jQuery );

2 个答案:

答案 0 :(得分:0)

如果每10次触发滚动事件触发ajax怎么样?

$.fn.scrollPagination.init = function(obj, opts) {
    var target = opts.scrollTarget;
    $(obj).attr('scrollPagination', 'enabled');
    target.scrollCount = 0;
    $(target).scroll(function(event) {
        this.scrollCount++;
        if (this.scrollCount % 10 == 0) {
            if ($(obj).attr('scrollPagination') == 'enabled') {
                $.fn.scrollPagination.loadContent(obj, opts);
                //alert(event.isPropagationStopped());
            }
            //event.stopPropagation();
            //console.log(event.isPropagationStopped());
            event.preventDefault();
        }
    });
}

答案 1 :(得分:0)

当滚动到达页面底部时,我习惯调用我的ajax函数。

function nearBottomOfPage() {
return $(window).scrollTop() > $(document).height() - $(window).height() - 200;
}

$(window).scroll(function(){      
    if (loading) {
      return;
    }

    if(nearBottomOfPage()) {
      loading=true;
      page++;
      $("#place_of_loading_image").show(); 
      $.ajax({
        url:'your source',
        type: 'get',
        dataType: 'script',
        success: function() {
            $("#place_of_loading_image").remove(); 
            loading=false;
        }
      });
    }
  });