Time Between two keyboard inputs in two sets

208 views Asked by At

It is hard to explain why, but I will try to explain what. Lets say this is speed tapping on keyboard. The faster you tap between two keys the higher your score will be (last part not implemented jet). Also It its better when you tap both keys one after other.

Idea is I am reading time from first key input and second key input time to calculate time between them - delta for first round. For future I want to compare first delta (tap one - tap two) with second delta (tap one - tap two). I can't get to compearing, becauca after first round of taps (tap one - tap two) i get both of deltas and most of the time they are equal.

1. What I am doing wrong? Should I just get all outputs and compare them later? 2. Also How can I deal with issue when I tapp one the same key twice. Id like to register, not ignore it.

    var start1 = 0;
    var satrt2 = 0;
    function run(){
    first();
    second();
    }
    function first(){
    document.addEventListener('keydown',function(event){
        if(event.keyCode == 37){

        start1 = new Date().getTime();
        //console.log("L " + start);
        }
        else if(event.keyCode == 39){
            var delta1 = new Date().getTime() - start1;
            console.log("first round input is " + delta1 );
            start1 = 0; 
        }
    });
    }

    function second(){
    document.addEventListener('keydown',function(event){
        if(event.keyCode == 37){

        start2 = new Date().getTime();
        //console.log("L " + start);
        }
        else if(event.keyCode == 39){
            var delta2 = new Date().getTime() - start2;
            console.log("Second round input is " + delta2 );
            start2 = 0; 
        }
    }); 
    }


    window.onload = run; 
1

There are 1 answers

3
Calummm On BEST ANSWER

I think this will work for the first part of your question.

var start1 = 0;
var start2 = 0;

function run(){
    first();
}

function first (){
    document.addEventListener('keydown',function(event){
        if (event.keyCode == 37){            
            start1 = new Date().getTime();
            
            if (start2) {
                console.log("first round input is " + (start1 - start2));
                start2 = 0;
            }
        
        } else if (event.keyCode == 39){
            start2 = new Date().getTime();
            
            if (start1) {
                console.log("second round input is " + (start2 - start1));
                start1 = 0;
            }

        }
    });
}

run()

I'm not really sure what you mean by your second part. Are you wanting to store every keypress and then output the difference between all start1 inputs when a start2 input is given? Could just store all presses with their timestamps in an array if this is the case.