How to create a jQuery timer that starts on keyup
I am using jQuery 1.4. I want create the timer which starts on keyup
event and resets on key press event. I want to use $.ajax
request to send the time interval to the server. All I could fin开发者_如何学运维d was countdown timers.
I appreciate any help.
You can do this very simply by comparing timestamps using native Javascript functions -- no jQuery necessary for the timing logic:
var start, end, interval;
$('form' /*or whatever selector*/).keydown(function() {
start = new Date();
}).keyup(function() {
end = new Date();
interval = end.getTime() - start.getTime(); // interval in milliseconds
// do your AJAX here
});
Try using a Date
object. Instantiate one on key down, and then another on key up and compare the two to find the time interval. This question has more detail.
精彩评论