等待获取请求完成

时间:2015-06-26 20:38:43

标签: javascript jquery

好吧,所以我正在制作一些可以创建账号的东西。我有这个函数检查用户名是否存在,如果不存在,则返回false。

然后我有另一个函数调用它,但它会自动转到else语句而不是等待get请求完成。我如何让它等到它得到回应?

var accountPassword = "accToGetTix"

function checkUsername(username){
    $.get("http://www.roblox.com/UserCheck/DoesUsernameExist?username=" + username, function(data){
        $( document ).ready(function() {
            return data.sucess; // true = taken , false = not taken
        });
    });
};
function makeNewAccount(accountName){
    if (checkUsername(accountName) == false){
        signupWindow = window.open("http://www.roblox.com/login/signup.aspx");
        signupWindow.$("#SignupUsername").val(accountName)
        signupWindow.$("#SignupPassword").val(accountPassword)
        signupWindow.$("#SignupPasswordConfirm").val(accountPassword)
        signupWindow.$('#birthdayMonthSelect option[value="0"]').prop('selected', true)
        signupWindow.$('#birthdayDaySelect option[value="0"]').prop('selected', true)
        signupWindow.$('#birthdayYearSelect option[value="25"]').prop('selected', true)
        signupWindow.$('.gender-circle').click();
    } else {
        return true; // true = account taken , false = not taken
    }
}

makeNewAccount('asdf205m0');

4 个答案:

答案 0 :(得分:2)

您可以通过更新代码来实现这一目标,如下所示

function checkUsername(username, callback) {
    $.get("http://www.roblox.com/UserCheck/DoesUsernameExist?username=" + username, function(data) {
        callback(data.sucess); // true = taken , false = not taken
    });
};


function makeNewAccount(accountName) {
    checkUsername(accountName, function(response) {
        if (response == false) {
            signupWindow = window.open("http://www.roblox.com/login/signup.aspx");
            signupWindow.$("#SignupUsername").val(accountName)
            signupWindow.$("#SignupPassword").val(accountPassword)
            signupWindow.$("#SignupPasswordConfirm").val(accountPassword)
            signupWindow.$('#birthdayMonthSelect option[value="0"]').prop('selected', true)
            signupWindow.$('#birthdayDaySelect option[value="0"]').prop('selected', true)
            signupWindow.$('#birthdayYearSelect option[value="25"]').prop('selected', true)
            signupWindow.$('.gender-circle').click();
        } else {
            return true; // true = account taken , false = not taken
        }

    });

}

答案 1 :(得分:0)

您无法通过异步调用return。你需要使用回调。一个小的重构,你可以使它工作:

function checkUsername(username, callback){
    $.get("http://www.roblox.com/UserCheck/DoesUsernameExist?username=" + username, function(data){
        if (callback) callback(data.sucess); // true = taken , false = not taken
    });
};

function makeNewAccount(accountName){
    checkUsername(accountName, function(response) {
        if (response === false) {
            signupWindow = window.open("http://www.roblox.com/login/signup.aspx");
            signupWindow.$("#SignupUsername").val(accountName)
            signupWindow.$("#SignupPassword").val(accountPassword)
            signupWindow.$("#SignupPasswordConfirm").val(accountPassword)
            signupWindow.$('#birthdayMonthSelect option[value="0"]').prop('selected', true)
            signupWindow.$('#birthdayDaySelect option[value="0"]').prop('selected', true)
            signupWindow.$('#birthdayYearSelect option[value="25"]').prop('selected', true)
            signupWindow.$('.gender-circle').click();
        } else {
            return true; // true = account taken , false = not taken
        }
    })
}

答案 2 :(得分:0)

你必须使用.done函数,如:

var accountPassword = "accToGetTix"

function checkUsername(username){
    $.get("http://www.roblox.com/UserCheck/DoesUsernameExist", {
        "username": username
    }).done(function(data){
        if (data.success) {
            alert("this username already exists");
        } else {
            makeNewAccount(username);
        }
    });
};

function makeNewAccount(accountName){
    if (checkUsername(accountName) == false){
        signupWindow = window.open("http://www.roblox.com/login/signup.aspx");
        signupWindow.$("#SignupUsername").val(accountName)
        signupWindow.$("#SignupPassword").val(accountPassword)
        signupWindow.$("#SignupPasswordConfirm").val(accountPassword)
        signupWindow.$('#birthdayMonthSelect option[value="0"]').prop('selected', true)
        signupWindow.$('#birthdayDaySelect option[value="0"]').prop('selected', true)
        signupWindow.$('#birthdayYearSelect option[value="25"]').prop('selected', true)
        signupWindow.$('.gender-circle').click();
    } else {
        return true; // true = account taken , false = not taken
    }
}

您可以在此处找到更多信息:https://api.jquery.com/deferred.done/

我不确定你的来电回复是什么,所以,如果你需要调整你的代码。如果需要,也可以在.done函数内使用console.log(data)。 :P

修改

jQuery中的$ .get函数向指定的URL发出Assynchronous http请求。 Assynchronous意味着代码不一定以线性顺序运行,等待请求完成以传递到下一个代码行。

javascript代码可以分为两种类型:同步和异步。让我举个例子。

这是一个同步代码:

function IWillRunFirst(){
    console.log("first!");
}

IWillRunFirst();

alert("I am the second");

var someElement = $("#justAnElement");

someElement.addClass("foo");

console.log("finish");

//this results:
//    "first!"
//    "I am the second"
//    "finish"

此代码将以与写入时完全相同的顺序运行。现在这里是一个同步代码的例子:

function IWillRunFirstButMyResponseWillGetLate(){
    $.get("someUrl", {
        "myUrlParameter": "foo"
    }).done(function(requestResponse){
        console.log("Howdy! Did I got late?");
    });
}

IWillRunFirstButMyResponseWillGetLate();

alert("I am the second");

var someElement = $("#justAnElement");

someElement.addClass("foo");

console.log("finished");

上述代码的结果可以是:

"I am the second"
"finished"
"Howdy! Did I got late?"

请记住:Ajax调用 总是 是同步的;)

答案 3 :(得分:0)

我认为最干净的方法是通过jquery deffered https://api.jquery.com/jquery.deferred/使用promises对象,同时你需要使用返回promises的jquery $ .get方法。 https://api.jquery.com/jquery.get/。 我可以分享代码示例,但是通过这个概念将使您受益更多。