This is a Rosetta Code post.
Story
You have a page on your website that displays data from the jsonplaceholder todos rest API. All 200 rows are displayed at once which is inefficient.
Task
Pagenate through n
results using infinite scrolling like a Facebook feed.
You must
- Use jQuery and javascript
- Paginate using
_start
(the page) and_limit
(the size) for each request. - When the user scrolls to the bottom of the screen fetch the next
n
results auto-magically \ :D /
The user will have the experience of infinite scrolling which will save their data and reduce unnecessary requests.
Solutions
Once the document loads fire the first get
, there after listen to the scroll event, use checkpos
to determine if the user is at the bottom of the screen, increment the start
variable and get
the next page.
Repeat this until the known max
is reached.
1 | let start = 0; |
Limitations include
- This will only fire when the user scrolls right to the bottom, a percentage of 95 could be used instead however the scroll event will need tweaking.
checkpos
is already doing a percentage calculation with this in mind - If the initial
get
returns less data than that which causes the y-axis scroll to display, the scroll listener will never fire - The API’s used in
checkpos
may not work in all browsers ¯\(ツ)/¯