$(function() {
var time = 0;
$("textarea").keyup(function() {
time = 0;
});
var int = self.setInterval(function() {
var s = $("textarea").val();
if (time++ == 3) {
if (s != "") alert(s);
time = 0;
}
}, 1000);
});β
Demo
UPDATE:
As @Tim Down said ,
The keypress event is designed for handling the a character typed by the user
rather than detecting keyboard activity and the delete and backspace keys
do not generate characters. Some browsers blur this line somewhat but
the general principle is that the keyup and keydown events are there to detect
any key being pressed and telling you about which key it is while keypress is
for detecting an actual character being typed.
So, it would be better if you used keyup instead of pressing a key, then your timer will clear for any key.
source
share