Question

I would like to force a scroll in javascript so i wrote that script but it doesn't work. The script makes the user scroll and after his execution, it goes back to the top of the page.

function myscroll(i)
{
    if (document.body.scrollTop < i - 100)
    {
        document.body.scrollTop += 10;
        setTimeout(myscroll(i), 10);
    }
}

How can i fix that ?

Thanks for reading.

Was it helpful?

Solution

You need to use an anonymous function for setTimeout to work as you'd expect:

setTimeout(function() {
    myscroll(i)
}, 10);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top