简体   繁体   English

如何从JavaScript中的连续函数返回布尔值?

[英]How can I return boolean value from consecutive functions in Javascript?

I want to learn, how can I return boolean value from consecutive functions in Javascript? 我想学习,如何从JavaScript中的连续函数返回布尔值? I want to run the next function, if previous function returns true. 如果上一个函数返回true,我想运行下一个函数。 For example there is a main function and there is 3 sub functions. 例如,有一个主要功能,有3个子功能。

It will be like this: run the first function if it returns true, run the second one and if the second returns true, run the third function if the third function returns true, then the main function also returns true. 它将是这样的:如果第一个函数返回true,则运行第一个函数;如果第二个函数返回true,则运行第二个函数;如果第三个函数返回true,则运行第三个函数,那么main函数也将返回true。

Can someone show me a simple example for that. 有人可以告诉我一个简单的例子。 I wrote an example but it always returns false :) I want to learn the logical best way for that. 我写了一个示例,但是它总是返回false :)我想学习逻辑上最好的方法。 Thanks all from now :) 从现在开始谢谢大家:)

My example: 我的例子:

var uname;

$("#username").keyup(function() {

    uname = checkUsername();
    console.log(checkUsername());

});     // Username keyup


function allowedChars() {

    var username = document.getElementById("username").value;

    var chars = /^[a-zA-Z0-9\.\_]*$/;

    if(chars.test(username) == true) {

        $("#notName").html("").show();

        return true;

    }

    else {

        $("#notName").html("Username can just contain A-Z, 0-9, dot and underscore!").show();

        return false;

    }

}   //  allowedChars

function unameLen() {

    var username = document.getElementById("username").value;

    if ((username.length > 3) && (username.length < 20)) {

        $("#notName").html("").show();

        return true;

    }

    else {

        $("#notName").html("Username must be between 3 and 20 characters!").show();

        return false;

    }

}

function unameAvl() {

    var username = document.getElementById("username").value;

    $.post("check.php", { username: username },
    function(result){

        if (result == true) {

            $("#notName").html("").show();

            return true;

        }

        else {

            $("#notName").html("Username is already exists!").show();

            return false;

        }

    });

}

function checkUsername() {

    allowedChars();

    if (allowedChars()) {

        unameLen();

    }

    if (unameLen()) {

        unameAvl();

    }

    if (unameAvl()) {

        return true

    }

    else {

        return false;

    }

}

You can use the && operator to do this for you, because it implements short-circuit boolean logic. 您可以使用&&运算符为您执行此操作,因为它实现了短路布尔逻辑。

Change your checkUserName method to this and see how it works: 将您的checkUserName方法更改为此,并查看其工作方式:

function checkUsername() {
    return allowedChars() && unameLen() && unameAvl();
}

In this case, when allowedChars returns true, it will next call unameLen . 在这种情况下,当allowedChars返回true,则接下来请unameLen If unameLen returns true, it will call unameAvl . 如果unameLen返回true,则将调用unameAvl The checkUsername function will return false if any of the functions return false, or true if they all return true. 如果任何一个函数返回false,则checkUsername函数将返回false;如果所有函数都返回true,则将返回true。

This will also fix the return value of unameAvl , so that it returns a boolean value, not undefined. 这还将修复unameAvl的返回值,以便它返回布尔值,而不是未定义。

function unameAvl() {
    var success = false;
    var username = document.getElementById("username").value;

    $.post("check.php", { username: username },
        function() {
            $("#notName").html("").show();

            success = true;
        }
    }).fail(function() {
        $("#notName").html("Username is already exists!").show();

        success = false;
    });

    return success;
}

jQuery's $.post does not expect a return value from the callback on success, so that return value is discarded. jQuery的$.post期望成功时不会从回调中获得返回值,因此该返回值将被丢弃。 The return value of unameAvl must be set from the function, so it must communicate that status through a variable ( success ). 必须从函数中设置unameAvl的返回值,因此它必须通过变量传达该状态( success )。

The arguments to the callback function are also incorrect. 回调函数的参数也不正确。 jQuery documents the signature of the callback function as Function(PlainObject data, String textStatus, jqXHR jqXHR) , however the actual function used had an incorrect signature of Function(boolean result) . jQuery将回调函数的签名记录为Function(PlainObject data, String textStatus, jqXHR jqXHR) ,但是实际使用的Function(boolean result)签名不正确。 The function arguments won't be used within the callback function, so the argument list can (and is) safely omitted. 函数参数不会在回调函数中使用,因此可以安全地省略参数列表。

The callback is only called on success, so a fail callback also needs to be provided. 仅在成功时才调用回调,因此还需要提供失败回调。 This is done via the $.fail method that's chained to the $.post call. 这是通过链接到$.post调用的$.fail方法完成的。

See these documentation pages: 请参阅以下文档页面:

All of these changes together make the Ajax call work and report success status correctly as the return value of unameAvl . 所有这些更改一起使Ajax调用正常工作,并正确地将成功状态报告为unameAvl的返回值。 This correctly allows the short-circuit logic to work as expected. 这样可以正确地使短路逻辑按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM