How to stop the loop for Javascript scroll down?

Viewed 138

I am using a javascript code for the F12 console to automatically scroll down. It works fine, but I want it to stop after 250 times. How to do that?

var scroll = setInterval(function(){ window.scrollBy(0,1000); }, 2000);
3 Answers

var count = 1;
var scroll = setInterval(() =>  this.executeInterval(), 10);

function executeInterval() {
   window.scrollBy(0,1000); 
   console.log(count)
   if(count == 250) clearInterval(scroll);

   count++;
}

Forgot the function. press on run code snippet.

You can use clearInterval() to cancel a repeating action which was established with setInterval().

Read the docs here :)

var scroll = setInterval(function(){ window.scrollBy(0,1000); }, 2000);

setTimeout(function() {
   clearInterval(scroll)
}, 250 * 2000)
Related