2013-02-20 80 views
0

我使用ajax从Web服务动态加载XML,返回的记录仅限于每个url'load'或'call'的25个项目。 ...为了解决这个问题,我有一个用户向下滚动页面的过程,当他们达到页面高度的90%(或者当他们到达页面底部时 - 不知道我会选择哪一个),一个名为startindexnum的变量由25如何检测何时页面滚动到底部并执行函数一次

增加,以致startindexnum开始了在25 ...那么函数的第一个“火”之后,startindexnum变为50,它成为75第三,等等,等等

我问题是它会多次触发并且有点不稳定 - 处理多次,当我滚动到底部,有时增加超过25(毫无疑问,我认为是多次运行的结果)。

任何人都知道我需要调整以获得此正确生成增量startindex变量追加到我检索XML的Ajax URL吗?谢谢。

var scrollcount = 1; 
var startindexnum = 25; 
var processing; 

$(document).ready(function(){ 
    $(document).scroll(function(e){ 
     if (processing) 
      return false; 

      window.onscroll = function(ev) { 

      if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight){ 
       //if ($(window).scrollTop() >= ($(document).height() - $(window).height())*0.9){ 
        // you're at x% of the page 
        processing = true; 
        scrollcount = scrollcount + 1; 
        startindexnum = scrollcount * startindexnum; 
        console.log(scrollcount); 
        docall(); 

        processing = false; 

       }; 
      }; 
    }); 
}); 
+0

使用解除绑定尝试()?表格再次渲染后 - 再次绑定该方法? – user1428716 2013-02-20 14:35:22

+0

btw ..你为什么要有window.onscroll和$ document.scroll? – user1428716 2013-02-20 14:37:28

回答

1

问题是我打赌docall()是一个异步呼叫,该呼叫经过这么设置的processingfalse无助于阻止未来的滚动事件。

在返回结果之前发生false设置。当docall()完成其任务时,您想要将processing设置为false。

  if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight){ 
      //if ($(window).scrollTop() >= ($(document).height() - $(window).height())*0.9){ 
       // you're at x% of the page 
       processing = true; 
       scrollcount = scrollcount + 1; 
       startindexnum = scrollcount * startindexnum; 
       console.log(scrollcount); 
       docall(); 

       //processing = false; <--get rid of this 

      }; 

function docall(){ 

    //When you are done fetching the new data and update the page set 
    function AjaxCallIsDone() { 
     processing = false; 
    } 

} 
+0

我认为这是正确的轨道....仍然不像我所需要的那样表现 - 我将更多地鼓捣它。感谢您的反馈 - 我真的很感激它。 – tamak 2013-02-20 15:16:51

0

除了epascarello的帖子...

你不需要的$(document).scroll(FN)和window.onscroll,你每次执行文档滚动处理程序时都会附加到它。几件事情:

1)首先,看看John Resig的这篇文章。 Scrolling by J.Resig

2)如果你想要jQuery的方法,然后使用窗口,而不是文档$(窗口).scroll(fn)。

3)如果没有,那么我认为下面会为你工作:

var scrollcount = 1; 
var startindexnum = 25; 
var processing; 

$(document).ready(function(){ 
    window.onscroll = function(ev) { 
     if (!processing) { 
      if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight){ 
       processing = true; 
       scrollcount = scrollcount + 1; 
       startindexnum = scrollcount * startindexnum; 
       console.log(scrollcount); 
       docall(); 
      } 
     }    
    } 
}); 
+0

我改变了ajax调用,所以它的非异步, $。(我会去研究它的任何未预料到的负面影响,但因为此刻没有人想到) 并且它在我身上发现了那些反常行为的一部分(数学是在哪里关),是由于这样的事实,这条线: startindexnum = scrollcount * startindexnum; 不得不硬编码使用25(在API返回的项目)的默认增量,所以它看起来像: startindexnum = scrollcount * 25; 谢谢大家。 – tamak 2013-02-20 16:44:42