调用函数和回调不起作用

时间:2017-06-26 15:21:44

标签: javascript jquery

所以我有一个检查Login的函数并运行一些不同的东西。然后我试图在页面的后面调用该函数,并在成功后执行另一个函数。也许我的结构不正确,虽然我已经尝试了相当多的东西,似乎什么也没有改变或者它破坏了我的代码。也许这不是最好的方法。任何想法。

function callLogin(callback) {
  if (document.getElementById("userLoggedIn") === null) {
    $(".cover").fadeIn(200, function() {
      $(".sixPinInputContainer").fadeIn(200, function() {
        $('.sixPinInput').first().focus()
        $("#pageBody").css("overflow", "hidden");
        var failedAttempts = 0;
        var password = "";
        $('.sixPinInput').keyup(function(k) {
          //DATA ENTERED INTO FIELD
          if (this.value.length == this.maxLength) {
            password = password + this.value;
            $(this).next('.sixPinInput').focus();
          }
          //PRESSING DELETE
          if (k.keyCode == 8) {
            $(this).prev().val("").focus();
            password = password.slice(0, -1);
          }
          password = password.substring(0, 6);
          $(".pinIncorrectMessage").css("display", "none");
        });
        $("#f").keyup(function(callback) {
          if ($("#f").val().length > 0) {
            $.post("http://heber/QC/includes/pinInput/getUserInfo.php", {
              user: password
            }, function(data, status) {
              password = "";
              $(".sixPinInput").val("");
              $(data).each(function(index, value) {
                var firstName = value.firstName;
                var color1 = value.color1;
                var color2 = value.color2;
                var authorizationLevel = value.authorizationLevel;
                var something = value.something;

                //IF SUCCESSFUL LOGIN
                if (firstName) {
                  $(".cover").fadeOut(200);
                  $(".sixPinInputContainer").fadeOut(200);
                  $("#pageBody").css("overflow", "scroll");
                  $("#userName").html(firstName);
                  $("#pageBody").css("background", "radial-gradient(at top left," + color1 + "," + color2 + ")");
                  $("#userSession").html("<div id='userLoggedIn'></div>");
                  password = "";
                }
                //IF FAILED LOGIN
                if (!firstName) {
                  $(".pinIncorrectMessage").css("display", "block");
                  $(".sixPinInput").first().focus();
                  $(".sixPinInput").val("");
                  $("#userName").html(firstName);
                  $("#pageBody").css("background", "radial-gradient(at top left," + color1 + "," + color2 + ")");
                  password = "";
                  failedAttempts = failedAttempts + 1;
                  if (failedAttempts >= 3) {
                    alert("If you forgot your password Please have it reset, You have " + (6 - failedAttempts) + " more Attempts");
                  };
                  if (failedAttempts >= 6) {
                    $(".cover").css("background-color", "black").css("z-index", "2000").css("cursor", "none");
                  }
                }
              })
            })
          }
        })
      });
    });
  };
  callback();
};

这是我调用该函数的地方,它在我的登录完成之前运行代码。

callLogin(function() {
    $("#" + rowID).load("eventHandlersPHP/updateStart.php", {
    roomID: id }, function(data, status) {
       $("#notStartedCount").load("eventHandlersPHP/jobsNotStartedCount.php"); 
    });
});  

1 个答案:

答案 0 :(得分:0)

尝试将callback的电话移至您的请求已完成的位置:

//IF SUCCESSFUL LOGIN
if (firstName) {
    $(".cover").fadeOut(200);
    $(".sixPinInputContainer").fadeOut(200);
    $("#pageBody").css("overflow", "scroll");
    $("#userName").html(firstName);
    $("#pageBody").css("background","radial-gradient(at top left," + color1 + "," + color2 + ")");
    $("#userSession").html("<div id='userLoggedIn'></div>");
    password = "";

    callback();
}
相关问题