js懶載入非同步作業解決辦法

來源:互聯網
上載者:User

標籤:++   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懶載入非同步作業解決辦法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.