0

There is a lot of examples about keypress, keyup,... in jQuery. These examples are about typing in text field. When user type a message or something in text field we can display something like User is typing but we can't display a message about situation when user don't type. Here is an example from http://api.jquery.com/keypress/:

$("#target").keypress(function() {
  $("#other).html("User is typing");
});  

<form>
  <fieldset>
    <input id="target" type="text" value="" />
  </fieldset>
</form>
<div id="other">
  Trigger the handler
</div>

How can I change this code and display message (user is not typing) when he don't type? Something like else option....

EDIT: Here is my idea. It is similar with yours idea (code). Thanks for helping.

$(document).ready(function(){
  $("#target").keyup(function(){
      setTimeout(function(){
          $("#other").html("User is not typing");
      }, 2000);   
  });
  $("#target").keydown(function(){
    $("#other").html("User is typing");
  });
});
geek_winner
  • 15
  • 1
  • 8

1 Answers1

1

I suggest using delay:

$("#target").keypress(function() {
      $("#other").html("User is typing");
      $("#other").delay(1000).queue(function(n) {
        $(this).html("User is not typing");
        n();
    });  
    });

http://jsfiddle.net/CbGL9/9/

Anthony
  • 427
  • 2
  • 6
  • A little optimized version: `$("#other").html("User is typing").delay(1000).html("");` – Cyclone Oct 10 '12 at 17:53
  • Your code works properly. But I ask you: What will happen after one second. Answer: There will always be a message: "User is not typing". – geek_winner Oct 10 '12 at 18:01
  • well I noticed .delay doesnt work with .html. I made an example of how it will work with .queue – Anthony Oct 10 '12 at 18:54