JQuery event processing queue

Problem Overview

In jQuery, the order in which you bind the handler is the order in which they will execute if you bind to the same element.

For example:

$('#div1').bind('click',function(){ // code runs first }); $('#div1').bind('click',function(){ // code runs second }); 

But what if I want the second related code to be run first?

.

My current solution

My current solution is to change the event queue:

 $.data(domElement, 'events')['click'].unshift({ type : 'click', guid : null, namespace : "", data : undefined, handler: function() { // code } }); 

.

Question

Is there something potentially wrong with my solution?

Is it safe to use null as a value for guid?

Thanks in advance.

.

+7
jquery jquery-plugins
source share
1 answer

There is an interesting plugin called eventstack . It allows you to bind events at either the top or bottom of the event queue. I don’t know how common your problem is, or you want to change or randomly arrange events on your stack, but a plugin can simplify your task.

If you want to collect a turn in a certain order, you can try the approach suggested in this answer - creating custom events that can trigger each other in turn.

Your approach may be the best for your requirement, but perhaps these are options.

+1
source share

All Articles