scrollTo分页调用过快



我使用scrollTop函数来检测用户离屏幕底部有多近,以便添加更多内容。我的javascript函数是这样的:

  if ($(window).scrollTop() >= $(document).height() - $(window).height() - 75) {
    var url = '<%=Url.Action("MoreView", "Status", new { id = ViewData["Id"], 
    page = Convert.ToInt32(ViewData["PageNumber"])+1 })%>';
            NextPage(url); //this does an ajax call to the action method on the server
        }

一切都很好,问题是窗户。ScrollTop>75有时会发生2到3次,速度如此之快,以至于它会加载相同的内容2或3次。我可能可以通过在服务器端使用会话变量来解决这个问题,但我不想走那条路,因为这会导致糟糕的IMO。有什么建议吗?

目前的情况是,您将始终向同一个URL发出请求(每次都会产生相同的数据,除非我不理解您在做什么)。一旦你解决了这个问题,你可能会想要包装你的函数,当用户滚动到一个debounce函数时调用它(我在这个例子中使用的是Underscore.js的debounce函数):

// Only call yourOnScroll once it hasn't been called
// for more than 100 milliseconds
yourOnScroll = _.debounce(youOnScroll, 100);

最新更新