Reset a function, or perhaps stop something and start it again?

I have this code:

$(document).ready(function() {  
    var number = 10;
    var offset = 10;
    var page_number = 2;    

    /* Bind the scroll function to an event */
    $(window).bind('scroll', function(e) {

        /* If the scroll height plus the window height is 
           more than the document height minus 10, continue */
        if($(window).scrollTop() + $(window).height() > 
           $(document).height() - 10) {         

            /* Quick message so you know more stuff is loading */
            $('.loading-more').html('Keep scrolling for more posts to load..');     

            $.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
                action: 'and_action',
                off: offset+number,
                pagenumber: page_number - 1
                }, function(data) {
                    offset = offset+number;                         
                    $('.empty-div').append('<p><strong>Page '+page_number+'</strong></p><br />'+data);
                    page_number += 1;               
                    $(this).unbind(e);                                          
            }); 
        }       
    });
});

This checks to see if the user is at the bottom of the page and is loading more content. The problem is that the user scrolls slowly around the critical point or quickly and quickly scrolls up and down, the function $.postruns several times, which means that you get several instances of the data that I load.

What I tried to do was a required and untied variable e, but it did not work so well. In any case, you can run the function postonce, and then have the reset function, so when the user scrolls again, it starts again, so more than one instance of the data is not loaded?

+3
3

: loading/ready.

$(document).ready(function () {
    var busy = false;

    $(window).bind('scroll', function (e) {
        if( !busy && goodposition){

           // load more
           busy = true;

           $.post(..., function(date){
             busy = false;
           });
        }
    });
});
+1

- :

var doingWork = false;

if(($(window).scrollTop() + $(window).height() > $(document).height() - 10) 
    && !doingWork) 
{
    doingWork = true;

reset doingWork false

+1

, , , . , , , . - .

$.post XMLHttpRequest, , .abort() :

$(window).bind('scroll', function(e) {
    if($(window).scrollTop() + $(window).height() > 
        $(document).height() - 10) {         

        $('.loading-more').html('Keep scrolling for more posts to load..');     

        if (curxhr && (curxhr.readyState != 4) { // if the curxhr object exists and has not completed
            curxhr.abort(); // abort the request
        }

        curxhr = $.post('<?php bloginfo('siteurl') ?>/wp-admin/admin-ajax.php', {
            action: 'and_action',
            off: offset+number,
            pagenumber: page_number - 1
            }, function(data) {
                // snip                                      
        }); 
    }       
});
0

Source: https://habr.com/ru/post/1773904/


All Articles