Skip to content
Advertisement

Block onClick events

Is it possible to block in some time frame (in the example 1s) onClick events after trigger?

0s: onClickEvent
200ms: Block the event
752ms: Block the event
1.2s: OnClickEvent
2.3s: OnClickEvent
2.6s: Block the event

How can I do without timers?

Advertisement

Answer

 const loadingTime = Date.now() + 1000;

 someEl.onclick = event => {
  if(Date.now() < loadingTime) event.stopPropagation();
};

Just prevent the event from propagating to other handlers.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement