Mr.Easy Answers McFly Mr.Easy Answers McFly - 13 days ago 7
TypeScript Question

Using lodash debounce in Angular2 scroll not working

I have the following code which calls a lodash debounce function:

@HostListener("window:scroll", [])
onWindowScroll() {
if (myBoolean)
_.debounce(this.onScrollDown(), 1500, false);

}
}

private onScrollDown() {
console.log("onScrollDown")
}


But I'm getting this error in my console:

Argument of type 'void' is not assignable to parameter of type 'Function'.


And the following on browser:

TypeError: Expected a function
at Function.debounce


I'm using typescript by the way

Answer

I'm not familiar with lodash, but I think what you want is:

private debouncedOnScroll = _.debounce(() => this.onScrollDown(), 1500, {});

@HostListener("window:scroll", [])
onWindowScroll() {
    this.debouncedOnScroll();
}

private onScrollDown() {
    console.log("onScrollDown")
}
Comments