I am trying to fire an event 3 seconds after the user has stopped typing to check the input to see if it meets the minimum length requirement. If it does, then show as valid and if less than 14 characters show as invalid/error. I want onkeydown to clear timer, check for positive validation, and if it fails to start another timer.
var typingTimer; //timer identifier
var doneTypingInterval = 3000; //time in ms, 3 second for example
// on keydown, start the countdown
$('#contact_phone').keydown(function(){
clearTimeout(typingTimer);
if ($('#contact_phone').val().length == 14) {
typingTimer = setTimeout(doneTyping, doneTypingInterval);
}
});
//user is "finished typing," do something
function doneTyping () {
$('#contact_phone').keyup(function() {
if ($(this).val().length == 14) {
$(this).removeClass('whiteBorder').removeClass('error').addClass('valid');
} else {
$(this).removeClass('whiteBorder').removeClass('valid').addClass('error');
}
});
}