滚动事件上异步更新scrollTop的值

时间:2018-10-07 13:40:09

标签: javascript jquery asynchronous callback lazy-loading

我正在使用滚动事件,并且在滚动中的某些点上,我将后端称为新数据。我在获取scrollTop当前值进行更新时遇到问题。我想念什么?我需要拥有scrollTop的新近更新的值,这样我才能不通过支票。似乎有些基本的东西,但我看不到...

这是代码示例:

var newspanel = $(".newspanel");
newspanel.on("scroll", function() {
var el = $(this);
var scrollTop = el.scrollTop();
var loading = false;

  if (scrollTop <= 1000 && !loading) { // I don't want to get into this if statement when getData callback is called and processed
    loading = true;
    var url = "some_url";
    getNewData(url, function(data) {
      var newScrollTop = 300; // There is logic that calculates new scrollTop
      el.scrollTop(newScrollTop); // Scroll is set further down, > 1000 in this example
      loading = false;
    })
  }

});

0 个答案:

没有答案