Ryan Dougherty
Ryan Dougherty

Reputation: 538

Simulate repeated clicks of button that refreshes page

I'm trying to execute JavaScript code that will simulate a repeated button click. Each click refreshes the webpage. Here is my code below:

for(i=0; i<1000; i++) {
    console.log(i);
    document.getElementById('button-id').click()
    window.onload = function () {  }
}

However, when I execute this, JavaScript prints out all 1000 values of i, then clicks the button only once.

Is it possible to "wait" until the page refreshes and then re-click the same button on the refreshed page?

Upvotes: 0

Views: 278

Answers (1)

user4257127
user4257127

Reputation:

In JavaScript, each time you reload the page, it resets the loops that you are in. Therefore, with JS alone, what you are asking for is not possible. However, you could (I'm not sure why you would) reload the page every time it loads.

It's really simple and I would never do this, but for educational purposes, here you go:

<body onload="location.reload()">

Hope this helps!

Upvotes: 3

Related Questions