2

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Josh R
  • 1,309
  • 6
  • 19
  • 31
  • Could you give a little more detail, please? I don't quite understand how `$.ajax` and timers fit together. – lonesomeday Nov 02 '10 at 08:44
  • I am currently working on a single field form. The form submits on keyup using jquery's $.ajax method. I also want to save the time interval between the key up and key press event. I was wondering if I could use jquery to find the time interval using jqeury. – Josh R Nov 02 '10 at 08:50

2 Answers2

2

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
});
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

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.

Community
  • 1
  • 1
Will Vousden
  • 32,488
  • 9
  • 84
  • 95