The display of lazyload + ajax when there is no data, invalid?

var $isFetchingNotes = false;

  $(window).scroll(function(){
    if($("-sharploader-blog").hasClass("nomore")) {
      return false;
    }

    var $scroll = $(window).scrollTop();
    var $docHeight = $(document).height();
    var $winHeight = $(window).height();

    if($scroll === $docHeight - $winHeight){

      if(!$isFetchingNotes) {

        $isFetchingNotes = true;

        $("-sharploader-blog").html("<img src="/images/lazyload40.svg">");

        var lastDiv = $(".data-display-blog:last");
        var lastId  = $(".data-display-blog:last").attr("id");
        var type  = $(".data-display-blog:last").attr("data-type");
        var query  = $(".data-display-blog:last").attr("data-query");
        var dataTo = "lastid="+lastId+"&type="+type+"&query="+query;

        $.ajax({
          type: "POST",
          url:"app",
          data: dataTo,
          cache: false,
          success: function(data){
            if(data != ""){
              lastDiv.after(data);
            }else{
              $("-sharploader-blog").addClass("nomore").html("nodata");
            }
          },
          complete: function () {
            $isFetchingNotes = false;
          }
        });

      }
    }
  });

when I find that scroll slips to the bottom, he will not run $("- sharploader-blog"). AddClass ("nomore"). Html (" nodata") even if the backend data is empty
;
this paragraph?
I added the alert event in my profile, but none of them responded.
but I use Google inspect to check < div id= "- sharploader-blog" > < / div > every time it slips, there will be a shock. (it may later be found that $("- sharploader-blog"). Html (""); )
how did this happen? I make sure that the value given at the back end is null.

even if I become like this:

if(data != ""){
              lastDiv.after(data);
            }else if(data == ""){
              $("-sharploader-blog").addClass("nomore").html("no-data");
            }

there is still no response.

charge:

how can I start issuing ajax requests before I hit the bottom?

Jul.29,2021

There is no problem with the visual inspection of the

code. Can you post the data returned by the back end, or add the following code under test:

// 
console.log(data);
if(data != ''){
    console.log("");
    lastDiv.after(data);
}else{
    console.log("");
    $('-sharploader-blog').addClass('nomore').html('nodata');
}

take a look at the results of the console. If the error cannot be located, it is recommended to post the console information

.

callback function after the completion of the complete: request (called after the request succeeds or fails). You should set $isFetchingNotes = false; in success when data='' . This should be the problem.


at present, there are two suspected problems in the code:

    The verb
  1. HTTP chooses POST, but uses GET stitching in the data. To be on the safe side, it is recommended to write as an object rather than string concatenation. In addition, you can also check the network panel to see whether the returned status code is normal 200or other
  2. .
  3. scroll is a high-frequency event. You can see in the code that the anti-shake is based on the flag bit, but I think it would be better to put the reset to the end of the success callback, or we can use the chained style of jQ, in the form of $.ajax (). Done (). Done () , the original success content is placed in the first done callback, and the flag bit is reset in the second done callback
  4. .
Menu