HiDayurie Dave
HiDayurie Dave

Reputation: 1807

How to prevent jQuery Load More Stop Function If Last Row has been showed

I have JS to load more post. The JS function is running OK, But when I scroll to bottom at last row, the function is always show DIV loading.

What I want is to prevent the function when it has reach last row.

Here is my JS :

<script>
var track_load = 0;
var loading  = false;
var total_groups = <?php echo $total_groups; ?>;

$('#content').load("load_post.php", {'group_no':track_load}, function() {track_load++;});
$(window).scroll(function()
        {               
            if($(window).scrollTop() + $(window).height() == $(document).height())
            {
                if(track_load <= total_groups && loading==false)
                {
                    loading = true;
                    $('.animation_image').show();

                    $.post('load_post.php',{'group_no': track_load}, function(data){

                        $("#content").append(data);

                        $('.animation_image').hide();

                        track_load++;
                        loading = false; 

                    }).fail(function(xhr, ajaxOptions, thrownError)
                    {
                        alert(thrownError);
                        $('.animation_image').hide();
                        loading = false;
                    });
                }
            }
        });

And here is my PHP :

<?php
$items_per_group = 10;
if($_POST)
{
    $group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

    if(!is_numeric($group_number))
    {
        header('HTTP/1.1 500 Invalid number!');
        exit();
    }

$position = ($group_number * $items_per_group);

//query
}

Upvotes: 1

Views: 169

Answers (1)

Joe Fitter
Joe Fitter

Reputation: 1309

Track load is 0 indexed. I imagine total groups is 1 indexed.

try if(track_load <= total_groups - 1 && loading==false)

Upvotes: 1

Related Questions