我的javascript代码没有按正确的顺序执行 [英] my javascript code is not executed in the right order

查看:72
本文介绍了我的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).

这篇关于我的javascript代码没有按正确的顺序执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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