0
votes

I'm developing a jQuery plugin which intercept several mouse events (mousedown, mouseup, mouseout, click) for different purposes.

However sometimes it doesn't work correctly due to one event fired before the other.

I can't really reproduce it, so please understand I made some research (for eaxmple I found http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-eventgroupings ).

How do I set mouse order firing (e.g. mouseup after click)? I can use jQuery if necessary.

1
The event order is always the same. What are you trying to do?Halcyon
A click is always a mousedown followed by a mouseup. If you are holding the mouse button down, then moving the cursor away before letting go, then it probably won't count as a click.Rocket Hazmat
But is mouseup guaranteed to fire before click, or vice versa?Matt Dunn
@Dunnie click is the complete action: mousedown + mouseup, so in essence you are expecting the same event to be fired when it comes to listening to mouseup events from both click and mouseupAGE
@AGE Not necessarily: you might want to bind one event to mouseup, and another to click. I can imaging there could be unusual scenarios where you want to do that.Matt Dunn

1 Answers

1
votes

As far as I'm aware, the ordering is browser dependent, so you can't rely on a specific sequence.

Would setting/checking flags work for you? e.g.

<button id="myButton">Click Me!</button>

<script>
    function doSomething() {
        alert("Hello world!");
    }

    var mouseUpFired = false;
    var clickFired = false;
    // etc

    $(document).mouseup(function() {
        mouseUpFired = true;
        if(mouseUpFired && clickFired /*etc*/) {
            doSomething();
        }
    });

    $(document).click(function() {
        clickFired = true;
        if(mouseUpFired && clickFired /*etc*/) {
            doSomething();
        }
    });
</script>