I want to delay action of .keypress()
method in jQuery for a certain amount of seconds.
I looked for the solution like:
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
and setInterval
,setTimeout
methods as well but in the first variant it freezes the entiry page what I really don't want to do. and in the second I don't know how to wrap my script into these functions.
My code sample :
$(document).ready(function() {
//some code
$(document).keypress(function(event){/*...this fragment I want to be freezed*/});
});
Question explanation:
Let's say that I have a simple counter which is in keypress
event handler counts how many times Enter button is pressed by user. And after the third click I want page not to response on user presses for 5 seconds without freezing the whole page(user still can click on a certain <button>
etc.. but any pressing on keyboard will do nothing untill 5 secs left(freeze keypress
event handler).