满足两个条件时,jQuery执行函数 [英] jquery execute function when two conditions are met

查看:423
本文介绍了满足两个条件时,jQuery执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当满足两个条件时,我需要执行特定的功能mvFinishItUp().更具体地说,一个条件是$.ajax的回调成功,另一个条件是代码到达函数之前的正常流程.有点:

I need to execute a specific function mvFinishItUp() when two conditions are met. More specifically, one condition is the callback success of a $.ajax the other is a normal flow of the code until it reaches the function. Kinda of this:

$.ajax({
    url: mv_finalUrl,
    success: function (data) {
        mvFinishItUp(data);
    },
    dataType: 'html'
});

/* here a lot more code, with animations and some delays */

mvFinishItUp(data) {
    /* My function code */
    /* But this code must only run after it has been called via the call back
       and after all the other code has been ran as well */
}

因此,如果ajax回调速度更快,或者相反,该函数必须等待所有代码.关于如何实施的任何想法?

So, the function must wait for all the code if the ajax callback is quicker, or the other way around. Any ideas on how this could be implemented?

我愿意更改脚本代码的整个概念,因为我相信ajax和函数本身之间的松散代码也应该归功于函数...

I'm willing to change the whole concept of script code, as I believe the loose code between the ajax, and the function itself should go to a function aswell ...

推荐答案

这是从AJAX调用中删除success:参数,并稍后注册处理程序:

Remove the success: parameter from the AJAX call, and register the handler later:

var jqxhr = $.ajax(...); 

// do some other synchronous stuff
...

// and *then* register the callback
jqxhr.done(mvFinishItUp);

在设计完成后,将延迟对象注册到Aem事件中( ).

Deferred objects cope perfectly well (by design) with being registered on an AJAX event after that event already finished.

这篇关于满足两个条件时,jQuery执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆