最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Infinitely scroll to the bottom of the page - Stack Overflow

programmeradmin4浏览0评论

I've been working on a script that scrolls to the dead end of my twitter follower [twitter/following] list automatically. So far, I've achieved this with my newbie knowledge:

var delay=2000       
setTimeout(function(){
    for(var i = 0; i<10; i++){   
    window.scrollTo(0,document.body.scrollHeight);
    }
},delay)

The problem the scroller stops after one scroll to the bottom and it doesn't scroll further when the page height changes. Is there a way to loop it with a delay so that scroll gets executed repeatedly until it reaches the last follower.

Alternatively, is there a way to do the same by constantly tracking and dynamically updating the height of the page.

I've been working on a script that scrolls to the dead end of my twitter follower [twitter./following] list automatically. So far, I've achieved this with my newbie knowledge:

var delay=2000       
setTimeout(function(){
    for(var i = 0; i<10; i++){   
    window.scrollTo(0,document.body.scrollHeight);
    }
},delay)

The problem the scroller stops after one scroll to the bottom and it doesn't scroll further when the page height changes. Is there a way to loop it with a delay so that scroll gets executed repeatedly until it reaches the last follower.

Alternatively, is there a way to do the same by constantly tracking and dynamically updating the height of the page.

Share Improve this question edited Mar 30, 2014 at 14:38 Ken asked Mar 30, 2014 at 13:28 KenKen 9022 gold badges15 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

I think you want to use setInterval instead of setTimeout.

setInterval will fire repeatedly instead of just the one time.

This works for me:

setInterval(function() { window.scrollTo(0, document.body.scrollHeight); },
            2000);

If you don't have a request delay when scrolling you can use:

function scrollToBottom() {
  // onscroll is only called when scrollHeight changes
  // avoiding infinit loop
  window.onscroll = () => {
    window.scroll(0, window.scrollHeight);
  }

  window.scroll(0, window.scrollHeight);
}
发布评论

评论列表(0)

  1. 暂无评论