我在滚动时调用 AJAX 函数:
// Collecting scroll info
$('.overthrow').each(function() {
var self = this;
var path2root = ".";
var subcategoryId = $(this).attr('data-subcategoryId');
var page = 1;
$(this).bind('scroll', function() {
var scrollAmount = $(self).scrollLeft();
var documentWidth = $(self).data('currentElementPosition') || $(document).width();
var scrollPercent = (scrollAmount / documentWidth) * 100;
if (scrollPercent > 80) {
page++;
console.log("process.php?subcategoryId=" + subcategoryId + "&page=" + page);
// AJAX function for processing JSON
$.ajax({
url: "process.php?subcategoryId=" + subcategoryId + "&page=" + page,
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
for(i = 0 ; i < data.length; i++) {
var articleId = data[i].articleResult.articleId;
var title = data[i].articleResult.title;
var subhead = data[i].articleResult.subhead;
var image = data[i].articleResult.image;
var response = "<td><div class='articleResult'><a href='" + path2root + "/article/article.php?articleId=" + articleId + "'><img src='" + image + "' width='120'/></a><br><h3><a href='" + path2root + "/article/article.php?articleId=" + articleId + "'>" + title.substring(0,25) + "</a></h3></div></td>";
var appendedData = $(self).find("table tbody tr td:last-child").after(response).fadeIn('slow');
$(self).data('currentElementPosition', appendedData.position().left);
};
}
});
};
});
});
我的问题是,一旦将第一个响应附加到内容中,并且我开始滚动超过 80% 到新结果中,我滚动的每个像素都会一次又一次地调用相同的 AJAX 函数,而不是等待用户滚动 80% 宽度的新窗口大小。有效地为滚动超过 80% 的每个像素进行 AJAX 调用。
声明是这样if
的错误路线吗?我是否应该提供某种类型的回调来动态驱动触发 AJAX 函数的整数?