码迷,mamicode.com
首页 > Web开发 > 详细

js懒加载异步操作解决办法

时间:2017-09-01 18:19:19      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:++   ajax   pre   result   prevent   pos   参数   事先   app   

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懒加载异步操作解决办法

标签:++   ajax   pre   result   prevent   pos   参数   事先   app   

原文地址:http://www.cnblogs.com/hhbbb/p/7464474.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!