我的javascript代码没有按正确的顺序执行
问题描述:
可能重复:
如何让javascript代码按顺序执行*
var cek = false;
function checkForm()
{
var user = document.forms["LoginForm"]["user"].value;
var pwd = document.forms["LoginForm"]["pwd"].value;
AJAXfunc("checkidpass.php?id="+user+"&pass="+pwd,function()
{
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
{
$("#LoginRes").html(xmlhttp.responseText);
if (xmlhttp.responseText == "")
cek = true;
}
}); //---> 1
return cek; //---> 2
}
我想问为什么return cek; (第2部分)在AJAXfunc(第1部分)之前执行?我想知道如何以正确的顺序执行它。
I want to ask why "return cek;" (part 2) is executed before the AJAXfunc (part 1)? And I want to know how to make it executed in the right order.
感谢您的帮助!
答
要了解您的问题,您需要了解异步方法。一个简单的谷歌将为您提供大量的阅读材料。我建议从这里开始:
To understand your issue, you need to understand asynchronous methods. A simple google will give you a plethora of reading material. I suggest starting here:
掌握Ajax,第2部分:使用JavaScript和Ajax发出异步请求
提示:您现在正在做什么从不工作 - 除非您选择将其设为同步功能(不推荐)。
Hint: What you're doing now will never work - unless you choose to make it a synchronous function (not recommended).