延迟一个keyup函数(作为jquery和ajax验证)

时间:2017-03-20 07:26:59

标签: javascript jquery ajax

var validate_email_login = function()
{
    console.log("validate-email");
    var item5 = $("#user_email2").val();
    var item5 = item5.toLowerCase();
    if (item5.length < 6 || item5.length > 50)
    {
        $("#errormsg6").html("Email : 6 - 50 Characters");
    }
    else
    {
        if (!emailformat.test(item5))
        {
            $("#errormsg6").html("Wrong Email Format");
        }
        
    }
}

var delay = (function() {
  var timer = 0;
  return function(cb, ms) {
    // ------------^
    clearTimeout(timer);
    timer = setTimeout(cb, ms);
    //-----------------^
  };
})();

$("#user_email2").keyup(function() {
  delay(function() {
   validate_email_login();
  }, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="email" class="homepage"  name="user_email2" id="user_email2" placeholder="Email" maxlength="50"  />

<div  id ="errormsg6"></div>

我有一个基于jquery和ajax验证的函数..我想延迟整个事情......在keyup上函数包括jquery以及ajax ......

HTML

<input type="email" class="homepage"  name="user_email2" id="user_email2" placeholder="Email" maxlength="50"  />

jquery和ajax

var validate_email_login = function()
{
    console.log("validate-email");
    var item5 = $("#user_email2").val();
    var item5 = item5.toLowerCase();
    if (item5.length < 6 || item5.length > 50)
    {
        $("#errormsg6").html("Email : 6 - 50 Characters");
    }
    else
    {
        if (!emailformat.test(item5))
        {
            $("#errormsg6").html("Wrong Email Format");
        }
        else
        {
            $.ajax(
            {
                type: 'POST',
                url: 'classes/validatelogin.php?f=1',
                data: "user_email2=" + item5,
                success: function(msg)
                {
                    if (msg == "ok")
                    {
                        $("#errormsg6").html("Email Does Not Exist");
                    }
                    else if (msg == "exists")
                    {
                        user_email2 = "1";
                        $("#errormsg6").html("");
                    }
                }
            });
        }
    }
}

延迟功能如下

var delay = (function()
{
    var timer = 0;
    return function(ms)
    {
        clearTimeout(timer);
        timer = setTimeout(ms);
    };
})();

$("#user_email2").keyup(function()
{
    delay(function()
    {
        validate_email_login;
    }, 1000);
});

但是我没有得到延迟...有什么我想念的东西我是javascript中的新手......

1 个答案:

答案 0 :(得分:1)

您需要将回调传递给延迟函数,并使用setTimeout调用它。

function validate_email_login() {
  //...stuff
}

var delay = (function() {
  var timer = 0;
  return function(cb, ms) {
    // -----------^
    clearTimeout(timer);
    timer = setTimeout(cb, ms);
    //-----------------^
  };
})();

$("#user_email2").keyup(function() {
  delay(validate_email_login, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="user_email2" placeholder="keyup"/>