开发者

jquery event to execute only when a particular event has finished

开发者 https://www.devze.com 2022-12-25 16:39 出处:网络
$(\'#div1\').focus(function () { callAnotherFunction(); $(this).animate({}); } I\'m trying to make $(this).animate({}); execute after call开发者_如何学编程AnotherFunction(); has completed. Currentl

$('#div1').focus(function () { callAnotherFunction();

  $(this).animate({});
}

I'm trying to make $(this).animate({}); execute after call开发者_如何学编程AnotherFunction(); has completed. Currently, both run at the same time. I've timed to use delay(), setTimeout() to no avail too. Is there anyway this can be done?

Thanks!


JavaScript is single threaded, so only one expression will be executed at a time. In the example that you've given, $(this).animate({}); will not run until callAnotherFunction(); has completed.

It's possible that callAnotherFunction(); runs additional code on a timer or delay, in which case you would have to have $(this).animate({}); on a timeout with an equal or greater delay for it to execute afterwards.


When in doubt, pass your function as a parameter:

$('#div1').focus(function () {
    var $t = $(this);
    callAnotherFunction(function(){ $t.animate(); });
}

... with callAnotherFunction looking something like this:

function callAnotherFunction(callback){
    // do whatever you were previously doing, and then...
    callback();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消