0

Let's say I have a <div>.

When onMouseUp fires, I do setTimeout(..., 0).

Is the onClick event guaranteed to fire before the timeout?

Lucien
  • 776
  • 3
  • 12
  • 40
  • 1
    click event will occur some ms after the mousedown - at the time of mousedown, there's no click event on the stack (as you haven't *moused-up*) so your code will always run first. @Ivar 's [fiddle](https://jsfiddle.net/1f7g5j4p/) also confirms this. – freedomn-m Jun 16 '22 at 08:44
  • 1
    @freedomn-m I actually got mixed up smh, and meant the mouse up event! The timeout does fire after for me but I'm wondering if this is guaranteed according to the spec – Lucien Jun 16 '22 at 08:46
  • 1
    Not sure about ` – freedomn-m Jun 16 '22 at 08:49
  • A quick [update](https://jsfiddle.net/swgLmap0/) the to the above fiddle shows that mouseup and click events are fired at the same time (assuming mouseup is within the time frame for a click so you do get a click). Thus, as per first (now deleted comment), there is a already a click event on the "stack" so your timeout code gets added after this and runs after the click event. You could always change to `setTimeout(..., 5)` to be sure as the user won't notice the difference. – freedomn-m Jun 16 '22 at 08:52
  • 1
    Note: there's a *minimum* period for setTimeout (SO answer somewhere) - anything below 4 is counted as 4. **edit** here's the question: [minimum value of stettimeout](https://stackoverflow.com/a/9647221/2181514) (though later (time-wise) comments appear to indicate this has been removed) – freedomn-m Jun 16 '22 at 08:52

0 Answers0