你如何确定一个异步函数在执行时(在很慢的电脑),另一个jQuery中前完成? [英] How do you determine if an asynchronous function is completed in before executing another one in jQuery (on really slow computers)?

查看:182
本文介绍了你如何确定一个异步函数在执行时(在很慢的电脑),另一个jQuery中前完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,假设我用.addClass()方法。你不能追加回调到它(例如,你不能做$('...')。addClass('...')。负载('...'))。

在大多数情况下,你可以打电话给你的功能背靠背使用.load(前)和你的罚款。

但如果我有一个超慢的电脑,其中采用.addClass()需要5秒了吗?我如何执行其他异步函数的只有的后.addClass()已经完成,或为此事,之后的任何的异步的JavaScript函数完成?

编辑:我错了,.addClass是的的异步

这样,那么,就可以肯定的:

  $('富')addClass('');
/ *此$ C $其中,C将不会执行,除非在previous线是绝对完了?* /


解决方案

addClass 是不同步的,并且没有环境中,它会采取任何显著的时间来执行它的工作。 jQuery的是各种操作的异步(AJAX,效果之类的话)接受回调作为API的一部分。

我的认为的(不回去,重新阅读API),如果一个API函数没有回调,则可以假定操作是同步的。

反过来是不正确的; jQuery有几个API函数(每个,例如)接受了的回调不的异步的,它只是回调是非常方便的其他原因

在任何情况下,任何异步操作应明确这样的文件中。

更新:解决您的更新问题:正确的,经过 addClass 的code不会执行,直到 addClass 已完成。即使花费一两秒钟(这当然不是这样,但我们pretend), addClass 将通过尽自己的事情迈着,并返回到您code。随后code将不会运行,直到它,并没有其他的code能而发生的事情(因为在JavaScript的Web浏览器是单线程的,除非你使用的网络工作者的,即使如此,线程之间的协调是明确的)。

有一些操作,你必须给浏览器的一个时刻来处理你告诉它做的事。我试图想到一个具体的例子中,周围的东西加表单域在IE中。绝对的不可以简单的事情。在那些罕见的情况下,可以确保你已经给浏览器有机会通过明确屈服于与浏览器做的事情的setTimeout

  doSomethingTheBrowserHasToThinkAbout();
的setTimeout(函数(){
    //现在你知道浏览器的有时间去想它
    doSomethingElse();
},0); //< ==不会真正为0,大多数浏览器夹住这5-10ms最低

但是你没有做到这一点(并且不希望这样做),除了某些特定的操作。对不起,我不能马上想起一个例子,但我们在这里谈论的边缘情况。

For example, let's say I use the .addClass() method. You can't append a callback to it (e.g. you can't do $('...').addClass('...').load('...') ).

In most cases, you can just call your functions back to back before using .load() and you're fine.

But what if I have a super slow computer where using .addClass() takes 5 seconds? How do I execute another asynchronous function only after .addClass() has finished, or for that matter, after any JavaScript asynchronous function is finished?

EDIT: I was wrong, .addClass is not asynchronous.

So, then, just to be sure:

$('foo').addClass('');
/*this code here will not execute UNLESS the previous line is absolutely finished?*/

解决方案

addClass is not asynchronous, and there is no environment in which it will take any significant time to perform its job. jQuery's various operations that are asynchronous (ajax, effects, that kind of thing) accept callbacks as part of their API.

I think (without going back and re-reading the API) that if an API function doesn't have a callback, you can assume the operation is synchronous.

The converse is not true; jQuery has several API functions (each, for instance) that accept callbacks that aren't asynchronous, it's just that the callback is handy for other reasons.

In any case, any asynchronous operation should be clearly identified as such in the documentation.

Update: Addressing your updated question: Correct, the code after addClass will not execute until addClass is complete. Even if it took a second or two (which of course it doesn't, but let's pretend), addClass will plod through doing its thing, and return back to your code. The subsequent code won't run until it does, and no other code can run while that's happening (because JavaScript on web browsers is single-threaded unless you use web workers, and even then coordination between threads is explicit).

There are some operations where you have to give the browser a moment to process what you've told it to do. I'm trying to think of a specific example, something around adding form fields in IE. Definitely not simple things. In those rare situations, you can ensure you've given the browser an opportunity to do its thing by explicitly yielding to the browser with a setTimeout:

doSomethingTheBrowserHasToThinkAbout();
setTimeout(function() {
    // Now you know the browser's had time to think about it
    doSomethingElse();
}, 0); // <== Won't really be 0, most browsers clamp this to 5-10ms minimum

But you don't have to do that (and don't want to do that) except for certain specific operations. I'm sorry I'm not immediately remembering an example, but we're talking edge cases here.

这篇关于你如何确定一个异步函数在执行时(在很慢的电脑),另一个jQuery中前完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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