JavaScript从函数返回。函数调用函数

时间:2017-04-20 14:48:14

标签: javascript

我试图更好地了解javacsript。而且我不确定为什么这段代码不起作用。我正在尝试创建将调用另一个函数的函数。并返回被调用函数的结果。

当我拨打下面的电话时,我会完全登录并显示我想要的屏幕。但是jsDidLogin始终返回undefined。有没有更好的方法来实现我的方法?

var jsDidLogin = beginLogin()    的console.log(jsDidLogin)

function waitUntilElementFound(element, time, callFunction) //Wait for the element to be found on the page
{
    if (document.querySelector(element) != null) {
        return callFunction();
    }
    else {
        if (!checkForFailedLogin()) {
            setTimeout(function () {
                waitUntilElementFound(element, time, callFunction);
            }, time);
        }
        else {
            return false;
        }
    }
}

function checkForFailedLogin() {
    if (document.querySelector("div[class='modal-body ng-scope'] h1") != null) {
        if(document.querySelector("div[class='modal-body ng-scope'] h1").innerHTML == "Login Error")
        {
          return true;
        }
    }
    else {
        return false;
    }
}

function initialTabSelect() //Load the bank page once login is completed
{
   document.querySelectorAll("li[class='Tab'] a")[0].click();
   return "Fully Logged In";
}
function initialDoNotAsk() {
    document.querySelectorAll("a[ng-click='modalCancel()']")[0].click();
    return waitUntilElementFound("li[class='Tab'] a", 1000, initialTabSelect);
}
function initialLogin() {
    var accountName = document.getElementById("username");
    var accountPassword = document.getElementById("password");
    var evt = document.createEvent("Events");

    evt.initEvent("change", true, true);
    accountName.value = "USERNAME";
    accountPassword.value = "PASSWORD";

    accountName.dispatchEvent(evt);
    accountPassword.dispatchEvent(evt);

    document.querySelectorAll("form[name='loginForm'] button.icon-login")[0].click();

    return waitUntilElementFound("a[ng-click='modalCancel()']", 2000, initialDoNotAsk);
}

function beginLogin() {
    return waitUntilElementFound("form[name='loginForm'] button.icon-login", 1000, initialLogin);
}

更改为此时会在完全登录时提醒我,但如果我将其更改为返回状态。我仍然没有回报。

我的头开始疼了:(

    function waitUntilElementFound(element, time, callFunction, callBack) //Wait for the element to be found on the page
    {
        if (document.querySelector(element) != null) {
            callBack(callFunction());
        }
        else {
            if (!checkForFailedLogin()) {
                setTimeout(function () {
                    callBack(waitUntilElementFound(element, time, callFunction, function(status){alert(status);}));
                }, time);
            }
            else {
                return false;
            }
        }
    }

function checkForFailedLogin() {
        if (document.querySelector("div[class='modal-body ng-scope'] h1") != null) {
            if(document.querySelector("div[class='modal-body ng-scope'] h1").innerHTML == "Login Error")
            {
              return true;
            }
        }
        else {
            return false;
        }
    }

    function initialTabSelect() //Load the bank page once login is completed
    {
       document.querySelectorAll("li[class='Tab'] a")[0].click();
       return "Fully Logged In";
    }
    function initialDoNotAsk() {
        document.querySelectorAll("a[ng-click='modalCancel()']")[0].click();
        return waitUntilElementFound("li[class='Tab'] a", 1000, initialTabSelect, function(status){alert(status)};);
    }
    function initialLogin() {
        var accountName = document.getElementById("username");
        var accountPassword = document.getElementById("password");
        var evt = document.createEvent("Events");

        evt.initEvent("change", true, true);
        accountName.value = "USERNAME";
        accountPassword.value = "PASSWORD";

        accountName.dispatchEvent(evt);
        accountPassword.dispatchEvent(evt);

        document.querySelectorAll("form[name='loginForm'] button.icon-login")[0].click();

        return waitUntilElementFound("a[ng-click='modalCancel()']", 2000, initialDoNotAsk, function(status){alert(status)};);
    }

    function beginLogin() {
        return waitUntilElementFound("form[name='loginForm'] button.icon-login", 1000, initialLogin, function(status){alert(status)};);
    }

0 个答案:

没有答案