1. 程式人生 > >[ECSHOP二次開發]解決分類商品Ajax連續請求導致的數據重復

[ECSHOP二次開發]解決分類商品Ajax連續請求導致的數據重復

代碼 ren += var 取數 開發 變量 images timeout

0x00:

首先聲明一個全局變量。

var control = true;

然後,在滑動處罰ajax請求的代碼處,做一個判斷。
if (control) {
    $(‘.get_more‘).click();
};

技術分享

技術分享

這個地方是獲取數據的函數以及ajax請求的函數

get_data: function() {
    var ile;


    control=false;    //首先進來這個函數之後吧全局變量設置成false以防重復請求


    lock = true;
    target.children(
".more_loader_spinner").css(‘display‘, ‘block‘); $(settings.trigger).css(‘display‘, ‘none‘); if (typeof(arguments[0]) == ‘number‘) ile = arguments[0]; else { ile = settings.amount; } if(variables.last >= cur_last){ var postdata = settings.data; postdata[
‘last‘] = variables.last; postdata[‘amount‘] = ile; $.post(settings.address, postdata, function(data){ $(settings.trigger).css(‘display‘, ‘block‘) methods.add_elements(data) control=true; //ajax請求完之後,重新吧全局變量設置回true,方便繼續請求下去。
//註意:根據測試這裏是要在lock的上面,否則失敗,根據目前觀察,是因為上面有判斷lock==false的條件,所以要吧全局變量放在loca的上面 }, settings.format) cur_last = cur_last+6; } }

0x01以上原理:

首先聲明一個全局變量設置為true,在觸發滑動時間的時候,判斷全局變量是否為true,如果為true則繼續觸發,否則不觸發, 然後在獲取數據,在Ajax請求數據前將全局變量設置成false,防止數據的重復請求,之後在Ajax執行完後,在吧全局變量改成true。 這個時候,就可以再次處罰滑動事件,如此的循環下去了。 其他完整示例代碼:

<script type="text/javascript">



    function dosearch(keyword){
        $(‘#keyword‘).val(keyword);
        getContent = get_next_page(1,<?php echo isset($_GET[‘cate_id‘])?$_GET[‘cate_id‘]:‘1‘;?>,1);
    }

    function select_all(){
        get_next_page(1,<?php echo isset($_GET[‘cate_id‘])?$_GET[‘cate_id‘]:‘1‘;?>,1);
    }

    $(function(){
        get_next_page(1);
    })

    var control=true;
    $(window).on("scroll",function(){
        if($(document).scrollTop()+$(window).height()>=$(document).height()){
            if(control){
                $(".static-box").html("<p>正在加載...</p>");
                $(document).scrollTop($(document).scrollTop()+100);
                get_next_page();
            }
        }
    });

    get_next_page = function (getpage,get_cate_id,setempty) {

        if(get_cate_id){
            $(‘#cate_id‘).val(get_cate_id)
        }

        var cate_id = $(‘#cate_id‘).val();
        var b_id = $(‘#b_id‘).val();
        var store_id = $(‘#store_id‘).val();
        var key = $(‘#key‘).val();
        var order = $(‘#order‘).val();
        var keyword = $(‘#keyword‘).val();

        if(getpage){
            page = 1;
        }else{
            var page = parseInt($(‘.order_page:last‘).val());
            page += 1;
        }

        var url = ‘index.php?act=mall&op=index&cate_id=‘+cate_id+‘&store_id=‘+store_id+‘&b_id=‘+b_id+‘&key=‘+key+‘&order=‘+order+‘&getact=next_page&curpage=‘+page+‘&keyword=‘+keyword;

        control=false;

        $.ajax({
            type:‘GET‘,
            url:url,
            data:‘‘,
            dataType:‘html‘,
            success:function(data){
                if(!data || data=="" || data == "clear"){
                    control=false;
                    if(data == ‘clear‘){
                        $(".minwrap").empty();
                    }
                    $(".static-box").html("<p>沒有更多數據了...</p>");
                    setTimeout(function(){
                        $(".static-box").html("");
                    },1000)
                    return false;
                }
                $(".order_page").remove();
                if(setempty == 1){
                    $(".minwrap").html(data);
                    $(‘html, body‘).animate({scrollTop:0}, ‘slow‘);
                }else{
                    $(".minwrap").append(data);
                }
                $("img.lazy").show().lazyload({
                    effect : "fadeIn",
                    threshold : 200
                });
                control=true;
            }
        });
    }
</script>

[ECSHOP二次開發]解決分類商品Ajax連續請求導致的數據重復