Alex
Alex

Reputation: 2062

Chrome extension - not able to re-start scrolling interval after initial stop

My extension needs to scroll to the bottom of the page smoothly, which works perfectly. The scrolling stops fine upon request.doScroll change to false. However, if I try to re-enable the scrolling it does not re-engage. Any ideas why and how can I achieve the stop\start and so on functionality?

//starts fine for the first time and does not start for the second time
if(request.doScroll){
            chrome.tabs.executeScript(sender.tab.id, {
                code:`
                    let x = 1; //y-axis pixel displacement
                    let y = 1; //delay in milliseconds
                    const t = setInterval(()=> {
                        window.scroll(0, x);
                        x = x + 5; //to increase speed increase increment interval

                        if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
                            console.log("clearing scrolling interval");
                            clearInterval(t);
                        }

                    }, y);
                    
                    `
            });
            }else{

                chrome.tabs.executeScript(sender.tab.id,
                    {
                        //this stops the scrolling
                        code: "clearInterval(t);"
                    }
                );
            }

Upvotes: 1

Views: 45

Answers (1)

Robbi
Robbi

Reputation: 1507

Try to change "if" block with this:

        code:`
            var t;
            var x;  //y-axis pixel displacement
            (_ => {
            let y = 1; //delay in milliseconds
            t = setInterval(()=> {
                window.scroll(0, x);
                x = (x||1) + 5; //to increase speed increase increment interval

                if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
                    console.log("clearing scrolling interval");
                    clearInterval(t);
                    x = 1   /* only if you want...*/
                }

            }, y);
            })()`

Upvotes: 1

Related Questions