Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debounce decorator to HostListener #294

Open
irustm opened this issue Mar 23, 2020 · 0 comments
Open

Add debounce decorator to HostListener #294

irustm opened this issue Mar 23, 2020 · 0 comments

Comments

@irustm
Copy link
Collaborator

irustm commented Mar 23, 2020

export function debounce(delay: number = 300): MethodDecorator {
  return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    const timeoutKey = Symbol();

    const original = descriptor.value;

    descriptor.value = function (...args) {
      clearTimeout(this[timeoutKey]);
      this[timeoutKey] = setTimeout(() => original.apply(this, args), delay);
    };

    return descriptor;
  };
}
@HostListener('window:scroll', ['$event'])  
@debounce() 
scroll(event) {
  ...
}

https://stackoverflow.com/a/54933830

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant