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



我需要一个javaScript,该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>

我的问题是我的掌握效果不佳。有时在页面末尾不会重新加载页面。

感谢您的帮助!

这应该做技巧:

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

相关内容

最新更新