How to run a function once when binding to multiple events that all trigger in Javascript?

前端 未结 5 1086
失恋的感觉
失恋的感觉 2020-12-19 20:23

I have a search input that listens to keyup and change to trigger an update of a listview via Ajax.

Looks like this:

input.on         


        
5条回答
  •  攒了一身酷
    2020-12-19 20:39

    If one is triggered first, then delete the other.

    const handler = e => {
      const eventType = e.type === 'mousedown' ? 'click' : 'mousedown';
      window.removeEventListener(eventType, handler);
    };
    
    window.addEventListener('click', handler);
    window.addEventListener('mousedown', handler);
    

提交回复
热议问题