November 2, 2010

How to create a jquery timer that starts on keyup

Question by Josh R

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 find was countdown timers.

I appreciate any help.

Thanks

Answer by lonesomeday

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
});

Answer by Starx

Why dont you check this link. It is a jquery stopwatch. Go through its coding, may be it will give you some idea.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!