Mark Tait
Mark Tait

Reputation: 643

Scroll down window using javascript

I'm trying to simulate a user pressing the pagedown key - and waiting for a second (waiting for the web page to show more results), simulate pressing pagedown again (waiting for the web page to show more) etc.

for(var s= 0;s< 5;s++){
window.scrollBy(0,500); // horizontal and vertical scroll increments
setTimeout(function() {}, 1000);
}

It just seems to trigger the page down once, with little/no delay.

I want the script to stop at the setTimeout and not execute anything else for a second, before carrying on.

Any advice would be great,

Thanks, Mark

Upvotes: 0

Views: 72

Answers (1)

Halcyon
Halcyon

Reputation: 57719

I think you want something like this:

function scrollDown(num_times) {
    num_times -= 1;
    if (num_times === 0) {
        return;
    }
    window.scrollBy(0,500); // horizontal and vertical scroll increments
    setTimeout(function() {
        scrollDown(num_times);
    }, 1000);
}

scrollDown(5); // scroll down 5 times

The setTimeout function needs to get the function you want to execute after the delay.

Upvotes: 2

Related Questions