Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8131005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:56:21+00:00 2026-06-06T08:56:21+00:00

I have a script that loads new products when a user scrolls to the

  • 0

I have a script that loads new products when a user scrolls to the bottom. The script works but I have two problems with it though. That’s
a) the script loads page1 twice
b) the script loads all pages at once instead of one at the time

Does anyone know a solution? I have limited knowledge so any help greatly appreciated.

My script

<script type="text/javascript">
$(document).ready(function(){

  // var pageSize = {{ collection.limit }};
  var currentPage = 1;
  var collectionPages = {{ collection.pages }};
  var category = '{{ collection.internal.url }}';

  // Appends the new product to the UI
  var appendProduct = function(product, id) {

  $('<div class="product"></div>')
    .html(product)
    .appendTo($(".productsGrid"));

  var i = 1;
  $('.product').each(function() {
    if(i++ % 3 == 0)
      $(this).addClass('last');
   });
  };

  // Load the next products page
  var loadProducts = function() {

    var url = "http://shop.com/"+category+"/page"+currentPage+".ajax";

    $.getJSON(url,function(data) {

      $.each(data.products, function(index, product) {
        appendProduct('<a href="'+product.url+'" title="'+product.fulltitle+'">' +
                    '<img src="'+product.image+'" width="180" height="150" alt="'+product.fulltitle+'" title="'+product.fulltitle+'"'+'</a>' +
                    '<div class="info"><h3><a href="'+product.url+'" title="'+product.fulltitle+'">'+product.fulltitle+''+'</a></h3>' +
                    '<div class="price">'+product.price.price_money+''+'</div>' +
                    '<div class="gridAddToCart"><a class="button grey" href="'+product.url+'" title="'+product.fulltitle+'" rel="nofollow">'+'<span>{{ 'Details' | t }}</span></a>' +
                    '<div style="margin-top:2px;"></div>' +
                    '<a class="opener button blue" href="http://meules1.webshopapp.com/cart/add/'+product.vid+'" title="'+product.fulltitle+'" rel="nofollow"><span>{{ 'Add to cart' | t }}</span></a>'
                   + '<div class="clear"></div>' +
                    '</div><div class="clear"></div></div>'      
        );
      });

    // We're done loading the products, so hide the overlay and update the UI
    $("#overlay").fadeOut();
    });
  };

  // First time, directly load the products
  loadProducts();

  // Append a scroll event handler to the container
  $(window).scroll(function() {
    // activate new load when scrollbar reaches 150 pixels or less from the bottom
    if($(window).height() + $(window).scrollTop() >= $(document).height() - 350) {

      if(currentPage <= collectionPages) {
        $("#overlay").fadeIn();
        loadProducts();
        currentPage++;
      }

    }
  });

});
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:56:23+00:00Added an answer on June 6, 2026 at 8:56 am

    Problem A

    You’re incrementing the page number after you run the loadProducts function:

    if(currentPage <= collectionPages) {
      $("#overlay").fadeIn();
      loadProducts();
      currentPage++
    }
    

    You should try this:

    if(currentPage <= collectionPages) {
      $("#overlay").fadeIn();
      currentPage++; //Moved this ABOVE loadProducts()
      loadProducts();
    }
    

    Problem B

    The code you execute at window.scroll is probably firing multiple many times when the user gets to the bottom, meaning all the pages are loaded straight away. One way to get around this could be to only run the code if your overlay is not visible:

    // Append a scroll event handler to the container
    $(window).scroll(function() {
      // activate new load when scrollbar reaches 150 pixels or less from the bottom
      if(($(window).height() + $(window).scrollTop() >= $(document).height() - 350) && $("#overlay").is(":hidden")) {
    

    Alternatively, you could set a global variable like var loading_page = false. Switch this to true when you start loading a page, switch it back to false once the new page is fully loaded, and only allow the scroll code to execute when the loading_page variable is false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that loads big images into the body in this fashion
I have a script that dynamically loads iframes inside a webpage. You can see
I have a matlab script, lets call it master.m, that loads a file called
This seems very strange to me but I have a script that I'm running
I have a php script that loads a CSS stylesheet, changes the color attribute
I have a PHP Script that loads an rss feed, parses it through Tidy
I have two files(html and js) as shown below. test.html <body> <script> /*Loads the
I have a PHP script that loads XML content dynamically: require_once 'directory/directory/'; $nice= '1149632';
I have a little script that allows my users to receive new items without
I have a script that loads info from external XML file and inserts it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.