jQuery trigger custom event synchronously?

99封情书 提交于 2019-11-27 15:08:05

You, my friend, are looking for jQuery "when".

http://api.jquery.com/jQuery.when/

To force anything to be synchronous, you can use something like this....

$.when($(this).trigger('custom-event')).done(function(){
    window.location.href = 'url';
});

Reading documentation about triggerHandler:

Instead of returning the jQuery object (to allow chaining), .triggerHandler() returns whatever value was returned by the last handler it caused to be executed. If no handlers re triggered, it returns undefined

This point in the documentation let me think that a code like this:

// Trigger the custom event
$(this).triggerHandler('custom-event');
// This code will only run when all events are run
window.location.href = 'url';

would meet your requirements.

See http://api.jquery.com/triggerHandler/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!