滚动处理程序错误

时间:2017-09-08 11:34:01

标签: javascript jquery ajax

我的scroll.window jquery脚本中有一个错误。脚本的功能是,当您作为访问者滚动到底部时,脚本会显示另外10个项目,但是有一个默认值。首先默认值为0到10,滚动时默认值为10到20,之后为20到30等。

但是剧本现在正在做的是,当我向下滚动时,我得到10到20,当我再次滚动时,我得到20到30和10到20.而我只需要20到30。

ng build --no-aot

}

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为您没有正确关闭getEvents()功能。在}之前添加另一个var counter = 1;,并在var win = $(window);函数之后添加getEvents



function getEvents(first = 0, second = 10) {
  // ajax call
  console.log(first);
  console.log(second);
}

var win = $(window);
// add counter 
var counter = 1;
// Each time the user scrolls
win.scroll(function() {
  // End of the document reached?
  if ($(document).height() - win.height() == win.scrollTop()) {
    getEvents(counter * 10 , (counter + 1) * 10 );
    counter += 2;
  }
});

html {
  height: 800px
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Scroll up and down
&#13;
&#13;
&#13;

完整代码:

function getEvents(first = 0, second = 10) {

$.ajax({
    dataType: 'json',
    url: '#',
    success : function(data)

    {
        console.log(first,second);
        $.each(data.slice(first,second), function(i, item) {

            content += '{html}';
            $(content).appendTo("#block-a-events");

        })
    }
});
}

var win = $(window);
// add counter 
var counter = 1;
// Each time the user scrolls
win.scroll(function() {
  // End of the document reached?
  if ($(document).height() - win.height() == win.scrollTop()) {
    $('#loading').show();
    getEvents(counter * 10 , (counter + 1) * 10 );
    counter += 2;
  }
});