Reputation: 1486
my code is like following: the problem is when I scroll to the end of the page,it will load data many times.if I have 10 elements in the page,and I load data from "/api/get_more_application/10",but before the loading done,jquery will send ajax to the "/api/get_more_application/10" second time before the dom added to the page,so maybe the solution should like,you can't send second request to url before the last request done,how to do that? thx
$(document).ready(function(){
var window_height = $(window).height();
var i = 1;
$(window).scroll(function () {
var page_height = $(document.body).height();
var scroll_top = $(window).scrollTop();
var delta = page_height-window_height-scroll_top;
if(delta<100){
random_number = Math.random();
dom_num = $(".item").size();
$.get("/api/get_more_application/"+dom_num+"?"+random_number,function(data,status){
var json_data = jQuery.parseJSON(data);
data_list_length = json_data.data_list.length;
if (data_list_length === 0){
$(".nodata").show();
}
$.each(json_data.data_list,function(){
var dict_data = {
"admin": admin,
"id": this.id,
"limit_save_number": this.limit_save_number,
"employeeid": this.employeeid,
"appname": this.appname,
"filetype": this.filetype,
"applicant_im": this.applicant_im,
"onlinestor_quota": this.onlinestor_quota,
"employee": this.employee,
"apply_content": this.apply_content
};
$.get("approveditem.html",function(html_data,status){
replaced_str = html_data.replace(/<<(\w+?)>>/g,function($0,$1){
var value = dict_data[$1];
return value;
});
$("#items_container").append(replaced_str);
})
});
});
auto_data_set();
};
});
});
Upvotes: 0
Views: 89
Reputation: 3090
Try jQuery's sleep function:
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
To call the function:
sleep(1000); // 1 second
Upvotes: 1