Last active
November 7, 2018 22:48
-
-
Save jportella93/6b0d45ab3e444e58bac1698d015bdb7c to your computer and use it in GitHub Desktop.
Scroll progresively to the bottom of page javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* scrollProgr.js | |
* Scroll progresively to the bottom of a page. | |
* | |
* param: jump Number: Pixels to jump in each scroll | |
* param: time Number: ms between each scroll | |
* param: maxDistance Number: Don't do more jumps after surpassing this number | |
* param: timeou Number: Kill the scrolling after this number of ms | |
* | |
* return <promise> undefined | String | |
*/ | |
function scrollProgr(jump, time, maxDistance=Infinity, timeout=null) { | |
return new Promise((resolve,reject) => { | |
let scrolledDistance = 0; | |
if (timeout) { | |
setTimeout(()=> { | |
resolve(`Scroll timed out after ${timeout}ms`) | |
clearInterval(scroll) | |
}, timeout) | |
} | |
return scroll = window.setInterval(() => { | |
let prevY = window.scrollY | |
window.scrollTo(0, window.scrollY + jump) | |
scrolledDistance += jump | |
let newY = window.scrollY | |
if (scrolledDistance >= maxDistance || prevY === newY) { | |
clearInterval(scroll) | |
console.log('Finished scrolling') | |
resolve() | |
} | |
}, time) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment