0

在下面的代码中,我在滚动时调用 ajax。但这是多次调用ajax。为了限制这一点,我添加了 setTimeout 函数和标志(即 isActive),它仍然调用了两次。
请帮助我哪里出错了。
提前致谢

var isActive = false;
var sIndex =12;
var myflag = '1';   
var offSet = 12;
var timeout;
jQuery(window).scroll(function () {
  if(typeof timeout == "number") {
  window.clearTimeout(timeout);
  delete timeout;
 }
timeout = window.setTimeout( check, 500);
});


function check(){
var cat = $(".mi-selected").attr('id');
var tecID = $("#technologyID").val();
    var notSameInd = $("#notSameInd").val();
    var sIndex =$("#startInd").val();
if (!isActive &&  ($(window).scrollTop() + $(window).height() == $(document).height()) && (sIndex !== notSameInd) ) {
   var isActive = true;
jQuery.ajax({
              type: "POST",
              url: 'http://some.com/responcePortfolio.php',
              data: {
             tecID:tecID,
                  cat:cat,
                  startIndex:sIndex,
                  offset:offSet,
                  count_now:count_now
               },
              success: function (result) {
            if(result !== ''){
                jQuery("#LoaderImage").hide();
                    jQuery("#portfolioList").append(result);
                $("#notSameInd").val(sIndex);
                    sIndex = parseInt(sIndex) + parseInt(offSet);
                $("#startInd").val(sIndex);
              }
                  else{    
                                   jQuery("#LoaderImage").hide();
               }
            isActive = false;
               },
              error: function (error) {
                  //alert(error);
              }
          });    

}

}

4

0 回答 0