There's a Chrome bug where click doesn't fire after a touchend: https://bugs.chromium.org/p/chromium/issues/detail?id=1141207#c10
I want to simulate a click using dispatchEvent:
let timer: number | null = null;
window.addEventListener('touchend', event => {
if (timer) {
clearTimeout(timer);
}
timer = window.setTimeout(() => {
event.target?.dispatchEvent(new Event('click', {
bubbles: true,
cancelable: true,
}));
}, 0);
});
window.addEventListener('click', () => {
if (timer) {
clearTimeout(timer);
}
});
I'm assuming it'll be impossible for the timeout callback to run before the click handler runs (if the click event occurs). With this assumption, I can make the timeout delay 0. If this assumption is false, then I'd need a longer delay, maybe 1-2 frames. Is click guaranteed to fire right after touchend, before touchend's event handler runs?
Are there