js懒加载异步操作解决办法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js懒加载异步操作解决办法相关的知识,希望对你有一定的参考价值。
ajax分页请求后台数据事先定义一个number
var number = 0;
var getBonusPointsList = function(id,num,key,sign){
var data2 = {
userId:id,
number:num,
accessKeyId:key,
sign:sign
};
data2 = JSON.stringify(data2);
$.ajax({
url:‘http://192.168.1.161:8080/user/getBonusPointsList‘,
type:‘POST‘,
data:data2,
/*header:{
‘Access-Token‘:token
},*/
dataType : ‘json‘,
contentType: "application/json",
success:function(data){
if(data.result){
var html = ‘‘;
totalPages = data.totalPages;
totalElements = data.totalElements;
if(data.last){
console.log(data.last);
last = true;
}else {
number = num+1;
}
for(var i =0; i<data.result.length; i++){
var time1 = data.result[i].recordTime;
var unixTimestamp = new Date(time1);
var timeStr = unixTimestamp.getFullYear() + "-" + (unixTimestamp.getMonth() + 1) + "-" + unixTimestamp.getDate() + " " + unixTimestamp.getHours() + ":" + unixTimestamp.getMinutes();
html = "<li><div class=‘list_left listText‘><div class=‘list_time‘>"+timeStr+"</div><div>完成一次充电</div></div><div class=‘list_right listText‘> +"+data.result[i].integralValue+"分 </div> </li>"
$("#intergral_ul").append(html);
}
}else{
console.log(data)
}
},
error:function(err){
//console.log(number)
alert(JSON.stringify(err))
}
});
}
getBonusPointsList("2",0,"","");
使用number == number2解决异步请求造成的参数不同步问题:
//懒加载
var number2 =1;
$(window).scroll(function (event) {
event.stopPropagation();
event.preventDefault();
if($(window).scrollTop() + 10>= ($(document).height() - $(window).height()) ){
if(last){
console.log(last);
}else if(number == number2){
console.log(number)
getBonusPointsList("2",number,"","");
number2++;
}
}
})
以上是关于js懒加载异步操作解决办法的主要内容,如果未能解决你的问题,请参考以下文章