简体   繁体   English

jQuery事件触发器; 关于动画的回调和时间安排

[英]jQuery event trigger; callback and timing with respect to animations

Perhaps my question deviates from the simplicity of itself: Given I .trigger() an event, how can I ensure that code following said .trigger() will not execute until the entire event handler function has completed, including all animations, delays, et al., therein? 也许我的问题偏离了它本身的简单性: 给定我.trigger()一个事件,我如何确保在整个事件处理函数完成(包括所有动画,延迟等.trigger()之前,执行上述.trigger()代码不会执行等。


I hope I'm missing something here; 我希望我在这里错过了一些东西。 I'm setting up a UI with a bunch of custom events. 我正在使用一系列自定义事件设置UI。 Some of the events are really just aggregates of other events; 有些事件实际上只是其他事件的集合。 for instance: 例如:

// ...
'cb-ui.hide': function(event){
    // do stuff to hide
},
'cb-ui.close': function(event){
    $(this).trigger('cb-ui.hide');
    // more stuff for close
},
// ...

Given there is an animation in the cb-ui.hide event, like .fadeOut(1500) , it appears ( in my testing ) that the remaining // more stuff for close doesn't wait for the animation to complete in the triggered event. 给定cb-ui.hide事件中有一个动画,如.fadeOut(1500) ,( 在我的测试中 )似乎剩余的// more stuff for close没有等待触发事件中的动画完成。 I was thinking ( previous to referencing the docs ) that .trigger() would likely have an optional callback argument much like the animation methods: 我在想( 参考文档之前.trigger()可能有一个可选的回调参数,就像动画方法一样:

$(this).trigger('cb-ui.hide', function(event){
    // more stuff for close
});

But this doesn't appear to be the case. 但这似乎并非如此。 Since event triggers are not blocking ( or don't appear to be at least ), what can I do to force the desired functionality, while keeping with the event handler/trigger implementation that I've been building off of? 由于事件触发器没有被阻止( 或至少看起来没有 ),我该怎么做才能强制实现所需的功能,同时又与我一直在构建的事件处理程序/触发器实现保持一致?


More specifically: 进一步来说:

$('[data-cb-ui-class="window"]').live({
    'cb-ui.hide': function(event){
        $(this).find('[data-cb-ui-class="content"]').animate({
            opacity: 0
        }, 1000);
    },
    'cb-ui.show': function(event){
        $(this).find('[data-cb-ui-class="content"]').animate({
            opacity: 1
        }, 1000);
    }
    'cb-ui.close': function(event){
        $(this).trigger('cb-ui.hide');
        $(this).find('[data-cb-ui-class="content"]').animate({
            height: 'hide' // happening simultaneously to the animation of 'cb-ui.hide'
                           // expected to happen in tandem; one after the other
        }, 1000);
    },
    'cb-ui.update': function(event, html){
        // none of this is working as expected; the expected being, the 'cb-ui.hide'
        // event is triggered (thus fading the [...-content] out) the HTML content is
        // updated, then the [...-content] is faded back in from 'cb-ui.show'
        // instead its just a mess that results in it fading out
        $(this).trigger('cb-ui.hide');
        $(this).find('[data-cb-ui-class="content"]').html(html);
        $(this).trigger('cb-ui-show');
    }
});

$('#foo').trigger('cb-ui.update', ['<p>Hello world!</p>']); // #foo is bound

This example animation should take ~2 seconds, but appears to be taking 1; 此示例动画应花费〜2秒,但似乎花费1; both animations are occurring simultaneous to each other, rather than in logical order. 两种动画是同时发生的,而不是按照逻辑顺序发生的。

Not sure if I understand your question right, but does this make sense? 不知道我是否理解您的问题正确,但这有意义吗?

You can just pass another function to be run after the animation is done. 动画完成后,您只需传递另一个要运行的功能即可。

'cb-ui.hide': function(event, callback){
    $('.lol').fadeTo(0,function() {
        // fire callback
    })
},
'cb-ui.close': function(event){
    cb-ui.hide(e,function() {
        // other stuff
    });
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM