在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 滾屏無刷新動(dòng)態(tài)加載數(shù)據(jù)問題?

滾屏無刷新動(dòng)態(tài)加載數(shù)據(jù)問題?

初始畫頁(yè)面時(shí)加載所有數(shù)據(jù):
$(document).ready(function(){ 
var counter=0;
    var pageStart=1;
    var pageSize=10;
    var isEnd=false;
    getData(pageStart,pageSize);
    function getData(offset,size){
        $.ajax({
            url: ,
            type: 'GET',
            async: true, 
            dataType:'json',
            contentType:"application/json; charset=utf-8", 
            success: function(data){
                respon=data.data.result;
                var sum=respon.length;
                for(var i=(offset-1)*size;i<sum;i++){
                    creatDiv(respon,i); //將數(shù)據(jù)依次填入新建的div中
                }
                if(((offset-1)*size+sum)===data.data.total){
                    $('.noMoreData').show().html("沒有更多數(shù)據(jù)了!");
                    isEnd=true;
                    return isEnd;
                  }
            },
            error: function(err){
            } 
        });
        $(window).scroll(function(){
            if(isEnd==true){
                return;
            }
         if($(document).height()-$(window).scrollTop()-$(window).height()<100){
                console.log("距離底部100px")
                pageStart++;
                getData(pageStart,pageSize);
            }
        })    
        

})
搜索時(shí)按條件加載:

function search(){
    $('.personInfo').empty();
    var counter=0;
    var index=1;
    var pageSize=10;
    var isEnd = false;
    searchData(index,pageSize);
    
    function searchData(pageIndex,eachPageSize){
        $.ajax({
            url:,
            type:'post',
            async:true,
            dataType:'json',
            data:{
                    index:pageIndex,
                    pageSize:eachPageSize,
                    name:$('#name').val(),
                 },
            success:function(data){
                if(data.status==0){
                    if(data.data.total!=0){
                        $('#total').empty();
                        $('#total').html(data.data.total);
                        respon=data.data.result;
                        if(respon.length!==0){
                            var sum=respon.length;
                            for(var i=0;i<sum;i++){
                              creatDiv(respon,i);
                            }
                            if(((pageIndex-1)*eachPageSize+sum)===data.data.total){
                                isEnd=true;
                         ('.noMoreData').show().html("沒有更多數(shù)據(jù)了!");
                            }
                            $(window).scroll(function(){
                                if(isEnd==true){
                                    return;
                                } if($(document).height()-$(window).scrollTop()-$(window).height()<100){
                                    index++;
                                    searchData(index,pageSize);
                                }
                            })
                        };
                    }else{
                       console.log("無符合條件的數(shù)據(jù)")
                }else{
                    console.log(data.message);
                };
            },
            error:function(data){
                console.log('err'); 
            }
        })
    } 
}

想要的效果是初始時(shí)顯示所有數(shù)據(jù),搜索時(shí)顯示出符合條件的數(shù)據(jù),兩者均動(dòng)態(tài)加載更多數(shù)據(jù),但現(xiàn)在搜索時(shí)滾動(dòng)條下滑時(shí)會(huì)觸發(fā)初始化的加載更多的接口,導(dǎo)致數(shù)據(jù)重復(fù)加載了

回答
編輯回答
故人嘆

很簡(jiǎn)單,定義一個(gè)全局變量。
當(dāng)初始加載的時(shí)候,讓變量等于true,
當(dāng)搜索的時(shí)候,變量為false,
然后,加載更多的時(shí)候判斷這個(gè)變量的狀態(tài)選擇執(zhí)行g(shù)etData()還是searchData();

2017年10月18日 13:55