Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Ajax data while page scrolls down using PHP

To load the data when page scrolls down using function like this

$(window).scroll(function(){
        if ($(window).scrollTop() == $(document).height() - $(window).height())
        {
            //alert('Scrolling Down');
            get_summary_details(); //Here it calls AJax Function load the data

        }
    });

get_summary_details() function works fine when page scrolls down.This function is like this

function get_summary_details()
    {

        var dataString=[];
        $('div.company_summary_data').each(function() {
            var id = $(this).attr('id');
            dataString.push(id);
        });                     
        $.ajax({
            url:"getajaxcompanysummarydetails",
            type:"POST",
            //dataType: "json",
            data:"last_app_data_id="+JSON.stringify(dataString),
            success:function(data)
            {                               
                $('.company_summary_data:last').after(data);

            }

        });         
    }

My problem is

  • while get_summary_details() processing the Request user will go to top of the page and Scroll down , again this get_summary_details() function will execute.

How to prevent that Second Request Processing without completion of first Request.Is this Possible? Because of this i am getting duplicate records of data.I need to prevent to display duplicate records.

Thanks!

like image 834
Krishna38 Avatar asked Oct 19 '22 14:10

Krishna38


1 Answers

Your AJAX requests are most likely queueing up behind one another, because they are asynchronous, even though JavaScript itself is mostly single threaded.

You can use the abort() method to make sure only one request runs at a time. You need to assign the jqXHR object returned by $.ajax() to a variable:

please refer this link

like image 132
MidhunKrishna Avatar answered Oct 27 '22 19:10

MidhunKrishna