简体   繁体   English

使用PHP向下滚动页面时加载Ajax数据

[英]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 当页面向下滚动时, get_summary_details()函数工作正常。这个函数是这样的

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. get_summary_details()处理请求用户将转到页面top并向下Scroll ,同样这个get_summary_details()函数将执行。

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! 谢谢!

Your AJAX requests are most likely queueing up behind one another, because they are asynchronous, even though JavaScript itself is mostly single threaded. 您的AJAX请求最有可能排在一起,因为它们是异步的,即使JavaScript本身大多是单线程的。

You can use the abort() method to make sure only one request runs at a time. 您可以使用abort()方法确保一次只运行一个请求。 You need to assign the jqXHR object returned by $.ajax() to a variable: 您需要将$.ajax()返回的jqXHR对象分配给变量:

please refer this link 请参考此链接

You need to check whether the ajax request is busy by setting a boolean flag 您需要通过设置布尔标志来检查ajax请求是否繁忙

var loadingSummaryDetails = false;

Set it to true when you start the Ajax and to false when the call finishes 启动Ajax时将其设置为true,并在调用结束时将其设置为false

function get_summary_details()
{
    if(loadingSummaryDetails) {
        return;
    }
    loadingSummaryDetails = true;

    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);

        }

    }).always(function()
        {     
            loadingSummaryDetails = false;
        });         
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM