Answer the question
In order to leave comments, you need to log in
Why doesn't the setTimeout delay work in the loop?
You need to smoothly rewind the page to the top by clicking. There is a delay of 100ms in the loop and the current position decreases by 10 pixels with each iteration. When you try to run the page jumps to the top instantly.
var position=window.pageYOffset;
while(position>0)
{
setTimeout(function (){window.scrollTo(0,position)}, 100);
position=position-10;
}
Answer the question
In order to leave comments, you need to log in
And you insert alert into the anonymous function that you pass to the setTimeout call. Everything will become clear right away. It's better to do this:
function smoothScroll() {
if(window.pageYOffset > 0) {
window.scrollTo(0, window.pageYOffset - 10);
setTimeout(smoothScroll, 100);
}
}
setTimeout(smoothScroll, 100);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question