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;

                };
            };
    });
});
4

2 回答 2

1

问题是我打赌docall()是一个异步调用,所以在调用之后设置processingtofalse不会阻止未来的滚动事件。

false 的设置发生在结果返回之前。您希望在完成任务后设置processing回 false 。docall()

         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;
    }

}
于 2013-02-20T14:25:56.307 回答
0

In addition to epascarello's post...

you don't need the $(document).scroll(fn) and the window.onscroll, which you are attaching to every time your document scroll handler is executed. A few things:

1) Firstly, take a look at this scroll post by John Resig. Scrolling by J.Resig

2) If you want the jquery method then use window instead of document $(window).scroll(fn).

3) If not then I think the following will work for you:

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();
            }
        }               
    }
});
于 2013-02-20T14:55:16.597 回答