1. 程式人生 > >js懶加載異步操作解決辦法

js懶加載異步操作解決辦法

tex point erro ica 一個 nts unixtime error: $.ajax

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懶加載異步操作解決辦法