Skip to content

Instantly share code, notes, and snippets.

@johannesMatevosyan
Last active May 6, 2026 09:25
Show Gist options
  • Select an option

  • Save johannesMatevosyan/9a5a2ee2567e82ae4d25451729b35cd9 to your computer and use it in GitHub Desktop.

Select an option

Save johannesMatevosyan/9a5a2ee2567e82ae4d25451729b35cd9 to your computer and use it in GitHub Desktop.
Reusable Angular directive for preventing rapid repeated clicks using RxJS (throttleTime). Designed to avoid duplicate API calls and unintended user actions while keeping default browser behavior intact.
import {
Directive,
DestroyRef,
EventEmitter,
HostListener,
Input,
OnInit,
Output,
inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Subject, throttleTime } from 'rxjs';
@Directive({
selector: '[appDebounceClick]',
standalone: true,
})
export class DebounceClickDirective implements OnInit {
@Input() debounceTime = 500;
@Output() appDebounceClick = new EventEmitter<MouseEvent>();
private readonly clicks$ = new Subject<MouseEvent>();
private readonly destroyRef = inject(DestroyRef);
ngOnInit(): void {
this.clicks$
.pipe(
throttleTime(this.debounceTime),
takeUntilDestroyed(this.destroyRef)
)
.subscribe((event) => this.appDebounceClick.emit(event));
}
@HostListener('click', ['$event'])
handleClick(event: MouseEvent): void {
this.clicks$.next(event);
}
}

Usage

<button
  type="button"
  appDebounceClick
  [debounceTime]="800"
  (appDebounceClick)="submitOrder()"
>
  Place Order
</button>

Use cases

  • Prevent double form submission
  • Avoid duplicate API calls
  • Order placement / trading actions
  • Any critical UI interaction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment