doulianqi3870 2015-08-28 18:23
浏览 49

无限滚动复制ajax

Some time when I refresh the page and scroll right the page loading it will duplicates the items. I'm using this code:

$(document).ready(function () {
    $('[data-toggle="tooltip"]').tooltip();
    //** My deals pagination starts**//
    var my_track_load = 0; //total loaded record group(s)
    var my_loading = false; //to prevents multipal ajax loads
    var my_total_groups = <? php echo $my_total_groups; ?> ; //total record group(s) 
    $('#load_deals').load("ajax/autoload_process.php", {
        'group_no': my_track_load,
        'my_deals': 'my_deals'
    }, function (data) {
        ++my_track_load;
    }); //load first group

    $(window).scroll(function () { //detect page scroll
        // if($(window).scrollTop() + $(window).height() == $(document).height())  //user scrolled to bottom of the page?
        //{
        if (my_track_load <= my_total_groups && my_loading == false) //there's more data to load
        {
            my_loading = true; //prevent further ajax my_loading
            $('.animation_image').show(); //show my_loading image
            //load data from the server using a HTTP POST request
            $.post('ajax/autoload_process.php', {
                'group_no': my_track_load,
                'my_deals': 'my_deals'
            }, function (data) {
                $("#load_deals").append(data); //append received data into the element
                //hide my_loading image
                $('.animation_image').hide(); //hide my_loading image once data is received
                my_track_load++; //loaded group increment
                my_loading = false;
            }).fail(function (xhr, ajaxOptions, thrownError) { //any errors?

                alert(thrownError); //alert with HTTP error
                $('.animation_image').hide(); //hide my_loading image
                my_loading = false;
            });
        }
        // }
    });
    //** My deals pagination Ends**//

    //** All deals pagination starts**//
    var track_load = 0; //total loaded record group(s)
    var loading = false; //to prevents multiple ajax loads
    var total_groups = <? php echo $total_groups; ?> ; //total record group(s) 
    var country = '<?php echo $country; ?>';
    var state = '<?php echo $state; ?>';
    $('#results').load("ajax/autoload_process.php", {
        'group_no': track_load,
        'country': country,
        'state': state,
        'all_deals': 'all_deals'
    }, function (data) {
        ++track_load;
    }); //load first group

    $(window).scroll(function () { //detect page scroll
        // if($(window).scrollTop() + $(window).height() == $(document).height())  //user scrolled to bottom of the page?
        //{
        if (track_load <= total_groups && loading == false) //there's more data to load
        {
            loading = true; //prevent further ajax loading
            $('.animation_image').show(); //show loading image
            //load data from the server using a HTTP POST request
            $.post('ajax/autoload_process.php', {
                'group_no': track_load,
                'country': country,
                'state': state,
                'all_deals': 'all_deals'
            }, function (data) {
                $("#results").append(data); //append received data into the element
                //hide loading image
                $('.animation_image').hide(); //hide loading image once data is received
                track_load++; //loaded group increment
                loading = false;
            }).fail(function (xhr, ajaxOptions, thrownError) { //any errors?
                alert(thrownError); //alert with HTTP error
                $('.animation_image').hide(); //hide loading image
                loading = false;
            });
        }
        // }
    });
});
  • 写回答

1条回答 默认 最新

  • doujianzi8521 2016-04-14 08:03
    关注

    I don't know if it helps you or not but below code might guide you somewhere.

    function FeedLoader(my_track_load,my_total_groups){
    
    $.ajax({            
        url:'ajax/autoload_process.php',
        type:"POST", 
        dataType:"text", 
        data:'my_track_load='+my_track_load+'&my_total_groups='+my_total_groups,                
        cache: false, 
        async: true, 
        success: function(data){
            if(my_track_load == 0)
                {
                    $(""#load_deals"").html(data);                  
                }
            else
                {
                    $(""#load_deals"").append(data);
                }           
        }, 
        complete: function(){                       
            $(window).on("scroll",function(){   
                var closeToBottom = ($(window).scrollTop() + $(window).height() > $(document).height() - 500);
                var AtBottom = ($(window).scrollTop() + $(window).height() == $(document).height());            
                if (closeToBottom || AtBottom)  
                    {       
                        if( my_track_load < total_m_group )
                            {   
                                my_track_load++;
                                FeedLoader(my_track_load,total_m_group);
                            }                   
                    }   
            });
        },              
        error:function (xhr, ajaxOptions, thrownError){                
            alert(thrownError);
        }
    }); }
    
    $(document).ready(function () {
     FeedLoader('0','<?php echo $my_total_groups;?>'); });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀