Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I understand your question , It's a common problem since the AJAX is asynchronous</p> <p>You can avoid the problem in two ways </p> <p><strong>1 - Setting the <code>async</code> Parameter to false</strong> </p> <pre><code>$(document).ready(function(){ $(window).scroll(function(){ /* window on scroll run the function using jquery and ajax */ var WindowHeight = $(window).height(); /* get the window height */ if($(window).scrollTop() +1 &gt;= $(document).height() - WindowHeight){ /* check is that user scrolls down to the bottom of the page */ $("#loader").html("&lt;img src='loading_icon.gif' alt='loading'/&gt;"); /* displa the loading content */ var LastDiv = $("#tabsul&gt;li:last"); /* get the last div of the dynamic content using ":last" */ var LastId = $("#tabsul&gt;li:last").attr("id"); /* get the id of the last div */ var offset = $("#tabsul&gt;li").length; //thats the size we have var ValueToPass = "Id="+ LastId; $.ajax({ /* post the values using AJAX */ type: "GET", url: "&lt;?= url('pages/ajax/All/')?&gt;"+offset+"/", data: ValueToPass, cache: false, async:false, success: function(html){ $("#loader").html(""); LastDiv.after(html); } }); } }); }); </code></pre> <p><strong>2 - Using boolean flags</strong> </p> <pre><code>var flag = true; $(document).ready(function(){ $(window).scroll(function(){ /* window on scroll run the function using jquery and ajax */ var WindowHeight = $(window).height(); /* get the window height */ if($(window).scrollTop() +1 &gt;= $(document).height() - WindowHeight){ /* check is that user scrolls down to the bottom of the page */ $("#loader").html("&lt;img src='loading_icon.gif' alt='loading'/&gt;"); /* displa the loading content */ var LastDiv = $("#tabsul&gt;li:last"); /* get the last div of the dynamic content using ":last" */ var LastId = $("#tabsul&gt;li:last").attr("id"); /* get the id of the last div */ var offset = $("#tabsul&gt;li").length; //thats the size we have var ValueToPass = "Id="+ LastId; if(flag == true) { flag = false; $.ajax({ /* post the values using AJAX */ type: "GET", url: "&lt;?= url('pages/ajax/All/')?&gt;"+offset+"/", data: ValueToPass, cache: false, success: function(html){ $("#loader").html(""); LastDiv.after(html); flag = true; } }); } } }); }); </code></pre> <p>In this model you set a variable to be true when it's done from the previous request </p> <p>Maybe you need to change the condition of the if statement if you want </p> <p>I hope this can help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload