Javascript Form How to - Grab text input data in 2 seconds delay








Question

We would like to know how to grab text input data in 2 seconds delay.

Answer


<!DOCTYPE html>
<html>
<body>
  <input type="text" id="txtName" onkeyup="exec(this.value)" />
<script type='text/javascript'>
<!--from   w  w  w. j a  va 2s .  c om-->
    var timer;
    exec = function(textData){
      clearTimeout(timer);
      timer = setTimeout(function(){ console.log(textData) }, 2000);  
    }

</script>
</body>
</html>

The code above is rendered as follows: