向下滚动一点,等待,然后重复到底部,然后稍等一下,然后重新加载Javascript页面

时间:2019-02-21 06:34:28

标签: javascript html

我需要一个可向下滚动的JavaScript,然后等待并重复此操作,直到到达网页末尾为止。然后等待并重新加载页面。到目前为止,这是我的脚本

<script>
        onbeforeunload = function () {
            window.scrollTo(0, 0);
        }
        onload = e=>{
            var d = document.documentElement;
            var offset = d.scrollTop + window.innerHeight;
            var height = d.offsetHeight;

            if (offset >= height) {
                setTimeout(function () {
                    location.reload(1);
                }, 5000);

            }
          setInterval(function(){
              scrollBy(0,100);
          }, 5000);
        };
        onscroll = function() {
            var d = document.documentElement;
            var offset = d.scrollTop + window.innerHeight;
            var height = d.offsetHeight;

            if (offset >= height) {
                setTimeout(function () {
                    location.reload(1);
                }, 5000);

            }
        };
    </script>

我的问题是我的scrypt无法正常工作。有时,它在页面末尾时不会重新加载页面。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

function autoScrollandReload(scrollDistance,pause){
    /// start the interval with the pause in between executions
    var interval = setInterval(function(){

      /// denife values, not necessary but easyier to read
      var scrolled = window.pageYOffset;
      var scroll_size = document.body.scrollHeight;
      var scroll_remaining = scroll_size-scrolled;;

      /// check if scrolling is necessary
      if(scroll_remaining <= window.innerHeight){
         clearInterval(interval);

         /// scroll back to top
         window.scrollTo(0,0);

         /// do here what you want
         setTimeout(location.reload(),500);

      }else{
         window.scrollBy(0,scrollDistance);
      };                

    },pause);

};

只需这样调用函数:

autoScrollandReload(100,5000);

这会每5秒将页面滚动100px