First off you shouldn't be using setTimeout
you should be using setInterval
. Basically because it fulfills your purpose better and it is more accurate as far as actually time. Google it if you want more info on that topic. But what you need is the clearInterval
method. Also it is faster to use anonymous functions inside of setTimeout
and setInterval
.
startPageScroll(){
var scrollAmt = 0;
var id=setInterval(function(){
if(scrollAmt</*Whatever your limit is*/){
window.scrollBy(0,50);
scollAmt+=50;
}
else{clearInterval(id);}
},100);
}
There are ways of getting the actual scroll of the page, but you would have to do something like this which is quite a bit more code then just keeping a running total of how far you have moved.
var scroll;
if(window.pageYOffset){
scroll=window.pageYOffset;
}
else{
scroll=document.body.scrollTop;
}
Here is a link that might help http://codepunk.hardwar.org.uk/ajs02.htm.