infinite scroll triggers the event too many times

Alberto

I'm trying to develop an infinet scroll with javascript and laravel but I'm finding that the javascript function when the scroll reaches the end is triggered multiple times, which makes it not work correctly.

The javascript code is this:

<script type="text/javascript">
        window.onscroll = function(ev) {
            if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
                window.livewire.emit('load-more');
                console.log('load more');
            }
        };
   </script>

Any idea why this is happening?

Steve O

You need to add a flag used to disable loading. Here we add a boolean isLoading to the window object:

<script type="text/javascript">
    window.isLoading = false

    window.onscroll = ev => {
      // Check scroll position
      if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
        // Check loading flag
        if(window.isLoading){
          console.log('still loading');
          return true
        }
        
        // Load more data
        window.livewire.emit('load-more');
        console.log('load more');

        // Set the flag to disable loading
        window.isLoading = true;
      }
    };
    
    // To reset the isLoading flag we'll emit an event from the Livewire component to the browser
    window.addEventListener('loading-complete', event => {
      console.log('loading complete');
      window.isLoading = false;
    })
</script>

As you can see above we reset the isLoading flag by emitting an event to the browser from the Livewire component (see the docs):

public function loadData()
{
    // Load data

    // Dispatch event to frontend
    $this->dispatchBrowserEvent('loading-complete');
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Scroll event firing too many times. I only want it to fire a maximum of, say, once per second

From Java

MigLayout - componentResized() called too many times

From Dev

TeamCity triggers too many builds for a new branch

From Dev

Updating scrollTop before scroll listener is assigned triggers a scroll event

From Dev

@HostListener cause change detection triggers too many times when I'm listening for outside click

From Dev

Python recursive function called too many times

From Dev

Ionic event fires too many times. Incremented by one each time it's triggered?

From Dev

Node .on method firing too many times

From Dev

Assembly loops loop too many times

From Dev

Function running too many times after adding event listener

From Dev

AWS Cloudfront redirects TOO_MANY times

From Dev

onLayoutChange() called too many times

From Dev

Using devise login as root calls separate root in an infinite loop and causes " url redirected you too many times"

From Dev

Request take too many times

From Dev

Keydown event triggering classList.remove doesn't work if the key is pressed too many times in a row

From Dev

Executing a file too many times?

From Dev

React event triggers multiple times

From Dev

Query on too many scroll contexts

From Dev

Infinite scroll in rails runs too many times

From Dev

android viewpagerindicator calling getPageTitle() too many times

From Dev

Knockout computed is triggered too many times

From Dev

File upload change event trigeeting too many times

From Dev

Process evaluated too many times

From Dev

Scroll down event 5 times

From Dev

Timer is executing too many times

From Dev

redirect too many times laravel

From Dev

Ionic 2 Infinite Scroll event never fires

From Dev

Nginx redirecting too many times

From Dev

How to prevent LSP onDidChangeContent event from firing too many times

Related Related

  1. 1

    Scroll event firing too many times. I only want it to fire a maximum of, say, once per second

  2. 2

    MigLayout - componentResized() called too many times

  3. 3

    TeamCity triggers too many builds for a new branch

  4. 4

    Updating scrollTop before scroll listener is assigned triggers a scroll event

  5. 5

    @HostListener cause change detection triggers too many times when I'm listening for outside click

  6. 6

    Python recursive function called too many times

  7. 7

    Ionic event fires too many times. Incremented by one each time it's triggered?

  8. 8

    Node .on method firing too many times

  9. 9

    Assembly loops loop too many times

  10. 10

    Function running too many times after adding event listener

  11. 11

    AWS Cloudfront redirects TOO_MANY times

  12. 12

    onLayoutChange() called too many times

  13. 13

    Using devise login as root calls separate root in an infinite loop and causes " url redirected you too many times"

  14. 14

    Request take too many times

  15. 15

    Keydown event triggering classList.remove doesn't work if the key is pressed too many times in a row

  16. 16

    Executing a file too many times?

  17. 17

    React event triggers multiple times

  18. 18

    Query on too many scroll contexts

  19. 19

    Infinite scroll in rails runs too many times

  20. 20

    android viewpagerindicator calling getPageTitle() too many times

  21. 21

    Knockout computed is triggered too many times

  22. 22

    File upload change event trigeeting too many times

  23. 23

    Process evaluated too many times

  24. 24

    Scroll down event 5 times

  25. 25

    Timer is executing too many times

  26. 26

    redirect too many times laravel

  27. 27

    Ionic 2 Infinite Scroll event never fires

  28. 28

    Nginx redirecting too many times

  29. 29

    How to prevent LSP onDidChangeContent event from firing too many times

HotTag

Archive