Javascript イベントを遅らせて発火させる
Javascript イベントを遅らせて発火させる
class DelayWork { constructor (callBackFunc, wait = 500) { this.callBackFunc = callBackFunc; // func.bind (this) this.wait = wait; this.timerID = null; } start (...arg) { if (! this.timerID) { let cbfunc = () => { this.timerID = null; this.callBackFunc (...arg); }; this.timerID = setTimeout (cbfunc, this.wait); } return this; } stop () { if (this.timerID) clearTimeout (this.timerID); this.timerID = null; return this; } } //_____________________________________________________ class DelayHandler { constructor (handler, wait = 300) { let cbFunc; switch (Object.prototype.toString.call (handler)) { case '[object Object]' : if ('handleEvent' in handler) if ('function' === typeof handler.handleEvent) cbFunc = handler.handleEvent.bind (handler); break; case '[object Function]' : cbFunc = handler; break; default: throw new Error ('Not handleEvent'); break; } this.handler = new DelayWork (cbFunc, this.wait); this.wait = wait; } handleEvent (event) { this.handler.stop ().start (event); } } //_____________________________________________________ function handler (event) { ; } document.querySelector ('input[type="search"]') .addEventListener ('input', new DelayHandler (handler), false);