[ECSHOP二次開發]解決分類商品Ajax連續請求導致的資料重複

來源:互聯網
上載者:User

標籤:setting   ade   eth   elements   nts   isset   val   element   二次   

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連續請求導致的資料重複

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.