skip to Main Content

I’m sending over 140 AJAX requests. The problem is that making 140 requests freezes the browser.

To fix this I want to make 10 requests and then on page scroll request 10 more.


    var url = "https://xxxxxxx.com/search?name=" + searchWord + "&xxxs=" + duck + "&format=application/json";
    jQuery.ajax({
      url: url,
      beforeSend: function() {
        jQuery('#image').show();
      },
      complete: function() {
        jQuery('#image').hide();
      },
      dataType: "text",
      success: function(data) {}
    });

I have tried this code:

var currentPageNumber = 1;
    loadMore(currentPageNumber);
$(window).scroll(function() {

    if($(window).scrollTop() ==  $(document).height()- $(window).height())
    {

        loadMore(currentPageNumber);
        currentPageNumber +=1;
    }
  });
function loadMore(currentPage){
        $.ajax({})
}

2

Answers


  1. I guess you are talking about loading data rather than requests.
    Its better to implement lazy loading and when we go the end of the page make a button stating “Load More” and again just increment it by 10.

    Login or Signup to reply.
  2. Let’s say you do this in google:

    https://www.google.com/search?q=duck
    

    You want next https://www.google.com/search?q=duck&start=10 so smth like:

    var currentPageNumber = 1;
    loadMore(currentPageNumber);
    $(window).scroll(function() {
    
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
    
            loadMore(currentPageNumber);
            currentPageNumber += 1;
        }
    });
    
    function loadMore(currentPage) {
        var start = currentPage * 10;
        $.ajax({
                url :'https://www.google.com/search', 
                data:{
                    'search': 'duck',
                    'start': start
                    },
                }).done(function(response) {
                    alert("Success !");
                    console.log(response);
                    }
                );   
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search