是jQuery和QUOT;每个()"功能同步? [英] Is jQuery "each()" function synchronous?

查看:119
本文介绍了是jQuery和QUOT;每个()"功能同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑此方案中用于验证:

consider this scenario for validating:

function validateForm (validCallback) {
   $('#first-name').add($('#last-name')).add($('#address')).each(function () {
      // validating fields and adding 'invalid' class to invalid fields.
   });
   // doing validation this way for almost 50 fields (loop over 50 fields)
   if ($('#holder .invalid').length == 0) {
       // submitting data here, only when all fields are validated.
   }
}

现在,我的问题是,在如果回路完成之前得到的块执行。我预计将同步执行 validateForm 的身体,但似乎jQuery的每()函数被异步执行。我对吗?为什么不起作用?

Now, my problem is that, the if block get executed before loops are finished. I expected the body of validateForm to be executed synchronously, but it seems that jQuery each() function gets executed asynchronously. Am I right? Why this doesn't work?

推荐答案

是的,jQuery的每个方法是同步的。几乎所有的JavaScript是同步的。唯一的例外是AJAX,定时器(的setTimeout 的setInterval ),和HTML5的Web工作者。结果
你的问题可能是别的地方在你的code。

Yes, the jQuery each method is synchronous. Nearly ALL JavaScript is synchronous. The only exceptions are AJAX, timers (setTimeout and setInterval), and HTML5 Web Workers.
Your problem is probably somewhere else in your code.

这篇关于是jQuery和QUOT;每个()"功能同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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