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 7664471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:18:52+00:00 2026-05-31T14:18:52+00:00

I have an annoying little issue here. I have code which loads content from

  • 0

I have an annoying little issue here. I have code which loads content from page2 and appends it to #content. That works, but sometimes it happens twice, then appends page3 before page2. Thats because in page3, there is just one post, but in page2, 4 posts.

How can I make my code wait until the append finishes to run again? Here’s my code:

$(window).scroll(
  function(){
    if(browserName != "safari") {
      var curScrollPos = $('html').scrollTop();
    }
    else {
      var curScrollPos = $('body').scrollTop();
    }

    if(curScrollPos > 218) {
      $("#sidebar").addClass("open");
    }

    if(curScrollPos < 218) {
      $("#sidebar").removeClass("open");
    }

    var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop();

    if(scrollBottom == 0) {
      if(home != 0 || search != 0 || category != 0) { 
        if(currentPage < numPages) {
          $("#main .loader-posts").fadeIn();
          currentPage++;

          if(search == 1) {
            var getPostsUrl = "page/"+currentPage+"/?s="+searchTerm;
          }
          else {
            var getPostsUrl = "page/"+currentPage;
          }

          $.get(getPostsUrl, function(data) {
                  $("#main .loader-posts").fadeOut(
                    'slow',
                    function(){
                      var newPosts = $(data).find("#content").html();
                      $("#content").append(newPosts);
                      $('#container.tiles').masonry('reload');
                    });
                });
        }
      }
      else {

      }
   }
  • 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-05-31T14:18:53+00:00Added an answer on May 31, 2026 at 2:18 pm

    Add a variable that keeps track of whether you are loading the next page or not and then only load if you are not currently loading something else. This is the root of your problem. .scroll() may fire twice before the next section is loaded, and as long as currentPage < numPages it will increment currentPage and load again. And, because .get() is asynchronous, you are not guaranteed to finish the first .get() request before any subsequent request. Adding a loading state fixes this.

    var loading = 0;  // Loading state
    
    if (scrollBottom == 0) {
    
        if (home != 0 || search != 0 || category != 0) {
    
            if (currentPage < numPages && !loading) {  // Only proceed if not loading
                loading = 1; // We have initiated loading
    
                $("#main .loader-posts").fadeIn();
                currentPage++;
    
                if (search == 1) {
                    var getPostsUrl = "page/" + currentPage + "/?s=" + searchTerm;
                } else {
                    var getPostsUrl = "page/" + currentPage;
                }
    
                $.get(getPostsUrl, function(data) {
                    $("#main .loader-posts").fadeOut('slow', function() {
                        var newPosts = $(data).find("#content").html();
                        $("#content").append(newPosts);
                        $('#container.tiles').masonry('reload');
                        loading = 0;  // We are done loading
                    });
                });
            }
        } else {
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an annoying SQL statement that seem simple but it looks awfull. I
I have a rather annoying issue here I can't get my CheckBox CheckedChange event
I have a little but annoying problem. I have made an app where the
XML is cool in flex, but I have an annoying problem to solve, caused
Hey I have a really annoying IE7 bug that I am trying to work
I have two questions. 1) I found a little gem of code for how
It is a little bit annoying to have the GWT hosted browser (on a
I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
I have a little problem and I'm hopping that you can help me solve
I've got a wierd/annoying little bug which I don't even know how to properly

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.