开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜