Landister
Landister

Reputation: 2224

Is there a way to set a timer in jquery

So what I want to do it start a page and if the user starts typing some input it shows that at the top of the page. But if the user stops typing input for lets say around 3 seconds it would automatically delete it.

Upvotes: 0

Views: 100

Answers (1)

David Hedlund
David Hedlund

Reputation: 129832

It's achievable in vanilla javascript, setTimeout.

setTimeout returns an ID, which you can use to clearTimeout if you don't want it to execute:

$(document).keyPress(function(e) {

   // do your thing

   // clear timeout that may have been set during previous key press
   window.clearTimeout(window.keyTimeout);

   window.keyTimeout = setTimeout(function() {
      // do your delete-thing
   }, 3000);

});

Upvotes: 4

Related Questions