如何确定jQuery对象是否延迟? [英] How can I determine if a jQuery object is deferred?

查看:130
本文介绍了如何确定jQuery对象是否延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个函数,有时会返回一个延迟对象,但有时会返回一个非延迟对象。我怎么知道它是哪一个?

If I have a function that sometimes returns a deferred object but sometimes a non-deferred object. How can I tell which one it is?

推荐答案

根据你的使用情况,你也可以使用 jQuery.when [1]:

Depending on your use case, you could also use jQuery.when [1]:


如果将单个参数传递给 jQuery 。当并且它不是Deferred时,它将被视为已解决的Deferred,并且任何附加的doneCallbacks将立即执行。

If a single argument is passed to jQuery.when and it is not a Deferred, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately.

使用 jQuery.when ,您可以将您的神秘对象始终视为延期:

With jQuery.when you can treat your mysterious object always as deferred:

// x could be a deferred object or an immediate result
var x = getMysteriousObject();
// success will be called when x is a deferred object and has been resolved
// or when x is an immediate result
jQuery.when( x ).then( success, error );

[1] http://api.jquery.com/jQuery.when/

这篇关于如何确定jQuery对象是否延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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