Answer the question
In order to leave comments, you need to log in
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');
Answer the question
In order to leave comments, you need to log in
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 =)
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question