【问题标题】:Resetting a function, or perhaps stopping something and starting it again?重置功能,或者可能停止某些东西并重新启动它?
【发布时间】:2010-11-09 17:26:47
【问题描述】:

我有这个代码:

$(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);                                          
            }); 
        }       
    });
});

这会检查用户是否靠近页面底部并加载更多内容。问题是如果用户在临界点附近缓慢滚动,或者一次又一次地快速上下滚动,$.post 函数会运行几次,这意味着您最终会得到一些我正在加载的数据实例.

我曾尝试绑定和解除绑定e 变量,但效果不佳。是否有可能运行一次post 函数,然后重置该函数,以便当用户再次向下滚动时它会再次运行,因此不会加载多个数据实例?

【问题讨论】:

    标签: javascript jquery pagination


    【解决方案1】:

    为什么不直接设置一个布尔值来表示当前状态:正在加载/准备就绪。

    $(document).ready(function () {
        var busy = false;
    
        $(window).bind('scroll', function (e) {
            if( !busy && goodposition){
    
               // load more
               busy = true;
    
               $.post(..., function(date){
                 busy = false;
               });
            }
        });
    });
    

    【讨论】:

      【解决方案2】:

      为什么不这样做:

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

      然后在您需要恢复功能时将doingWork 重置为false

      【讨论】:

        【解决方案3】:

        两个答案建议通过设置一个布尔值来记录请求是否存在。这大概就是我要走的路。另一种选择是取消现有请求。

        $.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                                      
                }); 
            }       
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-03-02
          • 1970-01-01
          • 2013-06-01
          • 1970-01-01
          • 2020-11-14
          • 1970-01-01
          • 2020-10-13
          相关资源
          最近更新 更多