故意暂停执行功能

pause executing function on purpose

本文关键字:功能 执行 暂停 故意      更新时间:2023-09-26

我有一个简单的.click()函数,里面有一些函数:

$mario.on('click', this, function() {
    var width_mario  = $window.width()/2;
    $contentw.animate({left: '100%', marginLeft: -width_mario, marginTop: 0}, 600);
    $fixed.css({'background-color': '#510000', 'left': '25%'});
    createMario();
    trigger(); // fire trigger not until createMario() is finished with animation
});

createMario()函数内部是大量 jQuery .animate() 离子。我打算做的是,在createMario()的动画完成之前触发触发器。类似于返回函数的东西,如果动画完成,您可以在单击函数中执行。

谢谢

编辑:而不是setTimeout()因为动画具有随机速度。

你不能

从字面上做到这一点。相反,您需要做的是对trigger函数的引用传递createMario中,并让它在动画完成后调用该函数。假设动画是通过jQuery制作的,它们在完成后会调用一个回调。

将其

作为回调传递,如下所示:

function createMario(calback_fn) {
    ...
    animate({...}, {duration: 12345, complete: callback_fn});
    ...
}

确保将其添加到最长的animate呼叫中。请注意,可能需要稍微更改动画语法,请参阅 http://api.jquery.com/animate/以了解不同的参数选项)。

然后做:

...
$fixed.css({'background-color': ... as before... });
createMario(trigger);

如果要对 createMario 函数中的 1 个元素进行动画处理,也可以使用 element.promise.done(function() { trigger(); });,也可以将其附加到最长的动画元素(完成时间最长的元素),它将在该元素完成动画处理后调用 trigger() 函数。

这是我整理的一个jsFiddle,它就是一个例子:http://jsfiddle.net/WPHmY/2/