Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue the `while` loop when ExecuteScript(setInterval) is finished

I have a code that scrolls a growing page to the bottom (until it's not possible to scroll to the bottom).
When it's not possible, it scrolls to the top and the javascript code is finished.

For example: imagine a timeline on facebook.
It's a growing page, so I can scroll it again and again until it's not possible to scroll (then I will be in: "BORN").

So this is my code:

while (i < elements.Count)
{

    js.ExecuteScript("var timeId = setInterval( function() { 
    if(window.scrollY<(document.body.scrollHeight-window.screen.availHeight)) 
    window.scrollTo(0,document.body.scrollHeight); else { clearInterval(timeId); 
    window.scrollTo(0,0); } },5000);");

    i++;
}

I want to add 1 to i only when the setInterval is finished.

I tried the next thing:

while (i < elements.Count)
{
    object a = js.ExecuteScript("setInterval( function() {  
    if(window.scrollY<(document.body.scrollHeight-window.screen.availHeight)) { 
    window.scrollTo(0,document.body.scrollHeight); return '1'}; else { 
    clearInterval(timeId); window.scrollTo(0,0); return '2'} },5000);");

    while (a != '2') {
      // do nothing, this while will be ended when we arrived the bottom and
      // go back to the top
    }

    // all the page is loaded
    i++;

}

but it doesn't work.. maybe there is a way to scroll to the bottom more and more and then to the top without using set interval? (but remember: it's a growing page that grows when you scroll it down and down..

How can I do it?

like image 835
Alon Shmiel Avatar asked May 13 '26 13:05

Alon Shmiel


1 Answers

The setInterval function is asynchronous, meaning that it happens after the ExecuteScript function gets a return value, this is why what you tried didn't work. The best solution I can think of is to change a little bit the structure of your code and use C# Threading.

Using C# Threading

This way, what we are going to do is to each time stop the code for 5 seconds and then execute a JavaScript code that checks if you can scroll further down, if yes scroll down and if not scroll back up. This JavaScript code will also return whether we should continue running this loop or not.

Basically, this is the JavaScript code we will execute:

if (window.scrollY < (document.body.scrollHeight - window.screen.availHeight)) {
    window.scrollTo(0, document.body.scrollHeight);
    return true;
} else {
    window.scrollTo(0, 0);
    return false;
}

And this is how the overall C# code should look:

while (i < elements.Count)
{
    bool run = true;
    while (run)
    {
        System.Threading.Thread.Sleep(5000);
        run = (bool)js.ExecuteScript("if(window.scrollY<(document.body.scrollHeight-window.screen.availHeight)){window.scrollTo(0,document.body.scrollHeight);return true;}else{window.scrollTo(0,0);return false;}");
    }
    i++;
}
like image 87
Cokegod Avatar answered May 16 '26 04:05

Cokegod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!