tags:

views:

18

answers:

2

Hi there

I wanna calculate the time between key 37 and 39 pressed. This is the left and right key.

So the user would press the two keys and i must calculate between each 2 key presses how long each gap was.

A: 

Use the Javascript Date object.

a = new Date();
b = new Date();
millisecondsInBetween = b - a;
Sjoerd
+1  A: 

Something like:

var start = 0;
$("#input").keyup(function(e) {
    if(e.keyCode == 37) {
        start = new Date().getTime();
    } else if(e.keyCode == 39) {
        var elapsed = new Date().getTime() - start;
        alert("elapsed time in milliseconds is: " + elapsed);
        // start again
        start = 0;
    }
});

Mess with it here: http://jsfiddle.net/9vmb4/1/

karim79