E
E
ennet2015-10-12 22:18:57
JavaScript
ennet, 2015-10-12 22:18:57

Callback when using .trigger() Jquery?

I have a number of click triggers. Clicks, in turn, call other methods and functions, including asynchronous ones. And there are moments when someone is called before the other.

$('.js-block_1').trigger('click');
$('.js-block_2').trigger('click');
$('.js-block_3').trigger('click');

Can you tell me how to make Callback ? According to the documentation, .trigger does not have a CallBack.
I came up with an idea with promises, but I can’t figure out where to write all this, because it’s just clicks. Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2015-10-12
@allard

The trigger just creates an event, it is not aware of what your handlers are doing next.
If you call events for a set of elements, then you are right about promises, you can’t do without them, because it is not clear when all handlers for all elements of the set will work...
If, however, you have a clear sequence of triggers and the elements are single and unique, then call the next trigger in the event callback triggered by the previous trigger.
I don't think I've said anything new to you.
Although there may be super hacks for such situations that we are not aware of =)

M
Maxim Martirosov, 2015-10-12
@kalbac

When called, the .trigger() method can accept input as an array.

(function( $ ){
$('selector').trigger('click', ['Custom']);
$('selector').on('click', function( event, data ){
if( data == 'Custom' ) {
console.log('Incoming data is Custom');
}
});
})(window.jQuery);

Thus, you can already understand which event worked and call the appropriate function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question