Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检测页面何时滚动到底并执行一次函数_Javascript_Jquery - Fatal编程技术网

Javascript 如何检测页面何时滚动到底并执行一次函数

Javascript 如何检测页面何时滚动到底并执行一次函数,javascript,jquery,Javascript,Jquery,我使用ajax从web服务动态加载XML,对于每个url“加载”或“调用”,返回的记录仅限于25项。。。。为了解决这个问题,我有一个用户向下滚动页面的过程,当他们达到页面高度的90%时(或者当他们到达页面底部时,我还不确定我会选择哪个),一个名为startindexnum的变量将递增25 所以startindexnum在25岁开始工作。。。然后在函数的第一次“开火”后,startindexnum变为50,第三次变为75,以此类推 我的问题是它会触发多次,而且有些不稳定——当我滚动到底部时会处理多

我使用ajax从web服务动态加载XML,对于每个url“加载”或“调用”,返回的记录仅限于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;

                };
            };
    });
});

问题是我打赌
docall()
是一个异步调用,因此在调用之后将
processing
设置为
false
不会阻止将来的滚动事件

在返回结果之前,设置为false。当
docall()
完成其任务时,您希望将
处理设置回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

            };

除了epascarello的帖子

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

1) 首先,看看约翰·雷西格的这篇滚动文章

2) 如果需要jquery方法,则使用window而不是document$(window).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();
            }
        }               
    }
});

是否尝试使用unbind()?再次呈现表后-再次绑定方法?顺便说一句。。为什么有window.onscroll和$document.scroll?我认为这是正确的。。。。不过,我的表现仍然没有达到我所需要的程度——我将对它进行更多的修补。感谢您的反馈-我真的很感激。我更改了ajax调用,使其非异步,$.ajax({async:false,(我会去研究它的任何未预见的负面影响,因为此时还没有想到)并且我明白了不稳定行为的那一部分(在数学上是不正确的)这是因为这一行:startindexnum=scrollcount*startindexnum;必须硬编码才能使用25(api中返回项的默认增量),所以它看起来像:startindexnum=scrollcount*25;多亏了all。
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();
            }
        }               
    }
});