question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scrollDown called many times (expected only 1)

See original GitHub issue

Hi, first thx to adapt infinite-scroll to angular2, my problem is when I scroll down, the onScrollDown() function is called more than one time, I want to call it only once see my code, is something strange?:

export class App implements OnInit{
  _contactsTotal:any; // get 500 data from server
  _contactsToRepeatInView:any;
  _waitData: boolean; //no ajax call if we are already waiting one

  constructor(private _contactService: ContactService) {}

  ngOnInit():any {
    this._contactsToRepeatInView = []
    this._waitData = false;

    this._contactService.getImages().map(res => res.json()).subscribe(data => {
      this._contactsTotal = data;
      this._contactsToRepeatInView = this._contactsTotal.splice(0,100); // show only 100 data from server response
    });
  }

  onScrollDown () {

    this._contactsToRepeatInView = this._contactsToRepeatInView.concat(this._contactsTotal.splice(0,100)) // when scrollDown concat() 100 additional data to actual view, from this._contactsTotal



    if(this._contactsTotal.length === 0){ //when this._contactsTotal equal 0 we do another ajax call and so the same stuff
      this._waitData = true; //this is problem
      if(this._waitData === true){ //this is problem
        this._contactService.getImages().map(res => res.json()).subscribe(data => {
          this._waitData == false; //this is problem
          this._contactsTotal = data
          this._contactsToRepeatInView = this._contactsToRepeatInView.concat(this._contactsTotal.splice(0,100))

        });
      }
    }
  }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
zoehnetocommented, Jul 26, 2016

Based on your example this should work:

export class App implements OnInit{
  _contactsTotal:any; // get 500 data from server
  _contactsToRepeatInView:any;
  _waitData: boolean; //no ajax call if we are already waiting one

  constructor(private _contactService: ContactService) {}

  ngOnInit():any {
    this._contactsToRepeatInView = []
    this._waitData = false;

    this._contactService.getImages().map(res => res.json()).subscribe(data => {
      this._contactsTotal = data;
      this._contactsToRepeatInView = this._contactsTotal.splice(0,100); // show only 100 data from server response
    });
  }

  onScrollDown () {
    this._contactsToRepeatInView = this._contactsToRepeatInView.concat(this._contactsTotal.splice(0,100)) // when scrollDown concat() 100 additional data to actual view, from this._contactsTotal

    if(this._contactsTotal.length === 0){ //when this._contactsTotal equal 0 we do another ajax call and so the same stuff
      if(this._waitData === false){
        this._waitData = true;
        this._contactService.getImages().map(res => res.json()).subscribe(data => {
          this._contactsTotal = data
          this._contactsToRepeatInView = this._contactsToRepeatInView.concat(this._contactsTotal.splice(0,100))
          this._waitData = false;
        });
      }
    }
  }
}
1reaction
zoehnetocommented, Jul 26, 2016

That’s because you always set _waitData to true before checking it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Scroll Function Firing Multiple Times Instead of Once
The scroll event (as well as the resize event) fire multiple times as a user does this. To help this, the best practice...
Read more >
module "scroll-down" handler is being called multiple times
Expected behavior: The scroll-down handler should only be called once, like before. If the new behaviour is required, it should be configurable, ...
Read more >
Automatic scrolling, only if a user already scrolled the bottom ...
In chat windows or similar growing lists, users often expect, that a page automatically scrolls to the latest item.
Read more >
Scrolling - The Modern JavaScript Tutorial
The scroll event allows reacting to a page or element scrolling. There are quite a few good things we can do here. For...
Read more >
SingleChildScrollView class - widgets - Flutter - Dart API docs
A box in which a single widget can be scrolled. This widget is useful when you have a single box that will normally...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found