UNPKG

mframejs

Version:
32 lines (24 loc) 922 B
import { behavior } from '../decorator/exported'; @behavior('debounce') export class DebounceBehavior { public name: string; constructor(binding: any, args: any) { const time = args[0] || 100; if (binding.eventHandlerBinded) { binding.eventHandlerBindedBackup = binding.eventHandlerBinded; binding.detachedBackup = binding.detached; binding.detached = function () { binding.detachedBackup(); }; binding.eventHandlerBinded = function () { if (this.timerBehavior) { clearTimeout(this.timerBehavior); this.timerBehavior = null; } this.timerBehavior = setTimeout(() => { binding.eventHandlerBindedBackup(); }, time); }.bind(binding); } } }