Javascript 执行顺序问题
我遇到了 Javascript 按照对我来说没有意义的顺序执行事情的问题。
正在发生的事情是以下
var bValid = false;
alert(bValid + " 1");
if(validateForm() == TRUE)
{
$.get("submit_manageUsers.php",
{action: sendAct, userID: userID},
function(responseText)
{
if(responseText == "GOOD")
{
alert("Update Successful");
bValid = true;
alert(bValid + " 2");
}
else
{
alert(responseText + "\n Update Unsuccessful");
bValid = false;
}
},
"html"
);
bvalid = true;
alert(bValid + " 3");
}
alert(bValid + " 4");
if(bValid == true)
{
//do something
}
alert(bValid + " 5");
编辑:添加了更多实际发生的事情,以防有帮助,可能会知道我是如何做事的!
上面代码的输出如下所示:
false 1
false 2
false 4
false 5
true 3。
这里的问题是 if(bValid == true)
在 if 之前执行(validateForm() == TRUE)
所以这意味着 bValid 始终为 false。
为什么这部分代码先于另一部分执行?
非常感谢对此的任何帮助!
I'm having an issue with Javascript executing things in an order that doesn't make sense to me.
What is happening is the following
var bValid = false;
alert(bValid + " 1");
if(validateForm() == TRUE)
{
$.get("submit_manageUsers.php",
{action: sendAct, userID: userID},
function(responseText)
{
if(responseText == "GOOD")
{
alert("Update Successful");
bValid = true;
alert(bValid + " 2");
}
else
{
alert(responseText + "\n Update Unsuccessful");
bValid = false;
}
},
"html"
);
bvalid = true;
alert(bValid + " 3");
}
alert(bValid + " 4");
if(bValid == true)
{
//do something
}
alert(bValid + " 5");
EDIT: added a bit more of what is actually happening in case it helps, probably will knowing how I do things!
The output from the above code looks like this:
false 1
false 2
false 4
false 5
true 3.
The problem here is that the if(bValid == true)
is being executed before the if(validateForm() == TRUE)
so this means bValid is always false.
Why is that part of the code executing before the other part?
Any help on this is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JavaScript 区分大小写。 validateForm()返回一个bool,对吗?所以它要么是真的,要么是假的。 TRUE 与 true 不相同。
我什至会更进一步,使用严格等于:
此外,bvalid 与 bValid 不同。
将来我建议使用众多 javascript 工具之一来帮助查找语法错误(例如 jsfiddle、jslint、jsbin 等)。我们都会犯错误,有时这些简单的小细节会引起大麻烦。尽管 YUI 和 Google 有语法检查工具可以提供帮助,但 JavaScript 并没有编译器拐杖(就像我每天使用 C# 一样)。
Google - http://code.google.com/closure/
雅虎! - http://developer.yahoo.com/yui/
在原始问题中提供更多信息后-
您正在对 PHP 页面进行异步调用,该调用可能需要 1 秒或 30 秒。
http://api.jquery.com/jQuery.ajax/
JQuery ajax(它支持.get 函数)默认是异步。但是,您可以覆盖它并使其同步。
Javascript is case sensitive. validateForm() returns a bool right? So its either true or false. TRUE is not the same as true.
I would even go one step further and use strict equals:
Also, bvalid is not the same as bValid.
In the future I would suggest utilizing one of the numerous javascript tools out there to help find syntax errors (such as jsfiddle, jslint, jsbin, etc.). We all make mistakes, and sometimes these little simple details cause big headaches. There is not a compiler crutch (like I use everyday with C#) for javascript although YUI and Google have syntax checking tools to help.
Google - http://code.google.com/closure/
Yahoo! - http://developer.yahoo.com/yui/
After more information was provided in the original question -
You are making an asynchronous call to your PHP page that may take 1 second or 30 seconds.
http://api.jquery.com/jQuery.ajax/
JQuery ajax (which powers the .get function) is asynchronous by default. However, you can override this and make synchronous.
您无需指定这些条件的 true 或 false。尝试这样的操作,看看是否可以解决您的问题:
You don't need to specify true or false on those conditions. Try something like this and see if it resolves your issue:
这样,脚本的第一部分应该在开始第二次验证之前完成。
希望这有帮助。
This way, the first part of your script should be done before it start the second validation.
Hope this help.
$.get
是异步的。这意味着您发送消息,挂断电话,然后等待 jQuery 在使用submit_manageUsers.php
完成后给您回电。步骤3和步骤4是在挂断电话后完成的,但步骤2是在回叫后才发生的,这可能需要一段时间。这就是你的消息乱序的原因。您必须将调用放入
回调内部,以便在正确的时间执行(即 AJAX 请求完成后)
$.get
is asynchronous. This means you send the message, you hang up the phone, and you wait for jQuery to call you back when it's done withsubmit_manageUsers.php
. Step 3 and Step 4 are done after you hang up the phone, but step 2 only happens after the callback is called, which may be a while. That is why your messages are out of order.you have to put the call to
inside of the callback for it to be executed at the correct time (which is after the AJAX request completes)