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

  • SEARCH
  • Home
  • 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 8973113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:22:31+00:00 2026-06-15T18:22:31+00:00

I load data into my DIV with AJAX using 3 different functions. I am

  • 0

I load data into my DIV with AJAX using 3 different functions. I am trying to prevent multiple AJAX calls when the browser scroller remains at the bottom of the DIV that I am lazy-loading data into.

This works but sometimes (just sometimes) the scroller remain at the bottom of my div and this will cause many AJAX calls to happen. How do I prevent that from happening?

$(document).ready(function() {

    //form submit by click
    $("#submit").click(function(e) {
        // Prevent Default Action In Case to Load data by form
        e.preventDefault();

        //prevent select to post empty data
        $('select').each(function() {
            if ($(this).val() == '') {
                $(this).attr('disabled', 'disabled');
            }
        });

        // Define what we need
        var loading = "<img src='/zojfa/images/loading.gif' alt='Loading...' />";
        var scrolltop = $('#scrollbox').attr('scrollTop');
        var scrollheight = $('#scrollbox').attr('scrollHeight');
        var windowheight = $('#scrollbox').attr('clientHeight');
        var post = $(this).attr("name") + "=" + $(this).val();
        var form_data = $('#search_form').serialize() + "&" + post;
        var scrolloffset = 20;

        //empty content if another value sent to code
        $('#content').empty();
        //load data
        loaddata(form_data, 0);
    });

    //listen to scroll function
    $('#scrollbox').scroll(function() {

        //define what we need
        var scrolltop = $('#scrollbox').attr('scrollTop');
        var scrollheight = $('#scrollbox').attr('scrollHeight');
        var windowheight = $('#scrollbox').attr('clientHeight');
        var scrolloffset = 20;

        //get number of div which will append to script in case of limit database to page 2 or 3 or...
        size = $('#content > div').children().size();



        //if we reach to bottom of div we are going to call to ajax function
        if (scrolltop >= (scrollheight - (windowheight + scrolloffset))) {
            var form_data = $('#formdata').val();

            //if remain of size(count of div) is 0 then we have more data to show because we limit data provided by script to 7 field(we handle situation that we had 14 or 21 respond from database in "next step" because if there is no data to show we dont have to let script to run)
            if (size % 7 == 0) {
                //call to load data function
                setTimeout(function() { loaddata(form_data, size) }, 1500);
            } else {
                //do nothing its just in case we need to append something like no more data to load
            }
        }
    });

    // page load finish
});


//function to load data
function loaddata(form_data, size) {
    number = "&size=" + size;
    //fetch new items
    $.post('dosearch.php', form_data + number, function(newitems) {
        //next step : if page echoing "" then do nothing
        if (newitems == '') {} else {
            //if we have data append these data to our div's #content
            $('#content').append(newitems);
        }
    });
}​

update

I did just as dear @Kent Pawar and dear @E.J. Brennan said but now I get more AJAX calls when I reach to bottom of the div and it still doesn’t quite work.

$("#submit").click(function(e) {
    // Do some Default
    e.preventDefault();

    $('select').each(function() {
        if ($(this).val() == '') {
            $(this).attr('disabled', 'disabled');
        }
    });

    // var what we need
    var loading = "<img src='/zojfa/images/loading.gif' alt='Loading...' />";
    var scrolltop = $('#scrollbox').attr('scrollTop');
    var scrollheight = $('#scrollbox').attr('scrollHeight');
    var windowheight = $('#scrollbox').attr('clientHeight');
    var post = $(this).attr("name") + "=" + $(this).val();
    var form_data = $('#search_form').serialize() + "&" + post;
    var scrolloffset = 20;

    $('#content').empty();

    //load data
    loaddata(form_data, 0);
    $('select').each(function() {
        if ($(this).val() == '') {
            $(this).removeAttr('disabled');
        }
    });
});


$('#scrollbox').scroll(function() {
    //var what we need
    var scrolltop = $('#scrollbox').attr('scrollTop');
    var scrollheight = $('#scrollbox').attr('scrollHeight');
    var windowheight = $('#scrollbox').attr('clientHeight');
    var scrolloffset = 20;

    // when we reach
    size = $('#content > div').children().size();

    if ($('#scrollbox').data('ajaxready') === false)
        return;

    if (scrolltop >= (scrollheight - (windowheight + scrolloffset))) {
        $('#scrollbox').data('ajaxready', false);
        var form_data = $('#formdata').val();

        if (size % 7 == 0) {
            setTimeout(function() { loaddata(form_data, size) }, 1500);
        } else {

        }
    }
    $('#scrollbox').data('ajaxready', true);

    // page load finish
});

function loaddata(form_data, size) {
    number = "&size=" + size;
    //fetch new items
    $.post('dosearch.php', form_data + number, function(newitems) {
        if (newitems == '') {} else {
            $('#content').append(newitems);
        }
    });
}
  • 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-15T18:22:32+00:00Added an answer on June 15, 2026 at 6:22 pm

    Well the issue isn’t with the scroller being at the bottom of the page but with the way the event handler works. There are times the scroller would be at the bottom of the page like say when there are no more post to load… observe Facebook’s wall for example.

    Currently, the JQuery scroll event is triggered when scrolling occurs.

    JQuery docs:

    A scroll event is sent whenever the element’s scroll position changes,
    regardless of the cause. A mouse click or drag on the scroll bar,
    dragging inside the element, pressing the arrow keys, or using the
    mouse’s scroll wheel could cause this event.

    Now its the job of your script to make a single AJAX call to check if there is content to be loaded. You need to modify your script to stop multiple AJAX calls from taking place during this time and as I see @E.J. Brennan has already suggested the same :).

    You could add the flags as follows:

    //listen to scroll function
      $('#scrollbox').scroll(function(){
    
            //[Kent] Before we service the event, we check if the last scroll event was handled/completed.
            //If it is not yet compelted, don't start another one and jump out of the code.
            if ($(window).data('ajax_in_progress') === true)
                return;
    
            //define what we need
            var scrolltop=$('#scrollbox').attr('scrollTop');
            var scrollheight=$('#scrollbox').attr('scrollHeight');
            var windowheight=$('#scrollbox').attr('clientHeight');
            var scrolloffset=20;
    
            //get number of div which will append to script in case of limit database to page 2 or 3 or...
            size =  $('#content > div').children().size();
    
            //if we reach to bottom of div we are going to call to ajax function
            if(scrolltop>=(scrollheight-(windowheight+scrolloffset))){
                $(window).data('ajax_in_progress', true);  //[Kent] prevent more scroll events as AJAX request will soon begin. 
    
                var form_data = $('#formdata').val();
    
                // if remain of size(count of div) is 0 then we have more data to show because 
                // we limit data provided by script to 7 field(we handle situation that we had 
                // 14 or 21 respond from database in "next step" because if there is no data to 
                // show we dont have to let script to run)
                if(size % 7 == 0){
                    //call to load data function
                    setTimeout(function(){loaddata(form_data, size)}, 1500);
                }else{
                    //do nothing its just in case we need to append something like no more data to load
                }
            }
        });    
    
    
    
    //function to load data
    function loaddata(form_data, size){
        number = "&size=" + size;
        //fetch new items
        $.post('dosearch.php', form_data+number, function(newitems){
            // [Kent] This is the callback funciton that gets executed ONLY
            // when the AJAX request has completed. We will append the data
            // into the DOM and then reset the FLAG.
    
            //next step : if page echoing "" then do nothing
            if(newitems == ''){
            }else{
                //if we have data append these data to our div's #content
                $('#content').append(newitems).each(function() {
                    //Adding a callback to append.
                    // So we reset the flags here to indicate the scroll event 
                    // has been handled/completed and so we turn scrolling events back on
                    $(window).data('ajax_in_progress', false);
                });
            }
        });
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Steps Get remote data using ajax (jQuery) I Load the data into div with
I was trying to load a data file into mysql table using LOAD DATA
I am trying to load some XML data into C++ code (classes) using gsoap.
I am using Jquery to get googlemap data and load it into a div.
I load 2 tables (header and data) dynamically into a div. I want the
I need to load data into my treestore. My ajax request give me XML
I'm using seeds.rb to load some dummy data into my project as I develop
I have a div into which I am loading data dynamically by ajax, jquery.
Using either .ajax or .load, I'm loading content from an .html page into or
I have a div: <div id=foo> </div> And I load data into it with

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.