JavaScript Detect time lapse between two key press in Javascript

another answer about your question,perhaps this is your truly answer.

 function duration(timestamps) {
        var last = timestamps.pop();
        var durations = [];
        while (timestamps.length) {
            durations.push(last - (last = timestamps.pop()));
        }
        return durations.reverse();
    }

    function display(mills) {
        if (mills > 1000)
            return (mills / 1000) + ' s';
        return mills + ' ms';
    }

    var durations = [];

    $('#in').keydown(function (e) {
        durations.push($.now());
    }).keyup(function (e) {
        var current = durations;
        current.push($.now());
        durations = [];
        var timeElapsed = current[current.length - 1] - current[0];

        console.log([
            ['time elapsed:', display(timeElapsed)].join(' '),
            ['keys duration:', duration(current).map(display)].join(' ')
        ].join(' --- '));
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
    <h2>Please input something to test!</h2>
    <input id="in"/>
</label>
Answer:1

Below is the sample code to measure the elapsed time between any 2 events. I added setTimeout just to give you an example.

var startTime = Date.now();

setTimeout(function(){
	var elapsedTime = Date.now() - startTime;
	console.log('elapsedTime ='+elapsedTime);
}, 100);
Answer:2

cautions: check key whether up in keydown() & notify keydown the key is upped in keypress();

   var start = null;
    $('#in').keydown(function (e) {
        if (!start) {//checking is a new user input
            start = $.now();
        }
    }).keyup(function (e) {
        var timeElapsed = $.now() - start;
        start = null;//start the next  timing tracking
         
         console.log(['time elapsed:', timeElapsed, 'ms'].join(' '));
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<h2>please input some letter to test</h2>
<input id="in"/>
</label>
Answer:3

i have a code that list below option in . when i select other, then combo box should convert into text. i tried with some script using onchange fucntion function chargeother(ele){ if(ele....

i have a code that list below option in . when i select other, then combo box should convert into text. i tried with some script using onchange fucntion function chargeother(ele){ if(ele....

  1. change combo box text size
  2. how to change text in combobox
  3. change combobox text color
  4. xaml change combobox text color
  5. jquery change text combobox

I am new to JavaScript and trying to find the difference between two JSON Objects. The structure of the JSON object and its data is shown below. I got a code online which works for a normal JSON ...

I am new to JavaScript and trying to find the difference between two JSON Objects. The structure of the JSON object and its data is shown below. I got a code online which works for a normal JSON ...

  1. compare json objects java
  2. compare json objects javascript
  3. compare json objects online
  4. compare json objects python
  5. compare json objects c#
  6. compare json objects nodejs
  7. compare json objects powershell
  8. compare json objects php
  9. compare json objects jackson
  10. compare json objects jquery
  11. compare json objects
  12. compare two json objects java
  13. compare two json objects javascript
  14. compare two json objects
  15. compare two json objects online
  16. compare 2 json objects java
  17. compare 2 json objects
  18. compare 2 json objects javascript
  19. compare two json objects python

I'm learning Angular 2. I'm using a LocationService with an Observable that hands me the coordinates after some time. This is my code. location.service.ts public getLocation(): Observable<any> ...

I'm learning Angular 2. I'm using a LocationService with an Observable that hands me the coordinates after some time. This is my code. location.service.ts public getLocation(): Observable<any> ...

  1. angular subscribe when to unsubscribe
  2. angular subscribe when done
  3. angular subscribe when
  4. angular subscribe when all
  5. angular subscribe when complete
  6. angular when subscribe finished

I'm new to js/programming and just making a simple calculator using prompt. I'm trying to validate that what's been entered are numbers and not strings. I tried this var operatorType = prompt("Do ...

I'm new to js/programming and just making a simple calculator using prompt. I'm trying to validate that what's been entered are numbers and not strings. I tried this var operatorType = prompt("Do ...

  1. if/else statement java
  2. if/else statement python
  3. if/else statement c++
  4. if/else statement javascript
  5. if/else statement code.org
  6. if/else statement r
  7. if/else statement excel
  8. if else statement
  9. if else statement in c
  10. if else statement java
  11. if else statement python
  12. if else statement in c example
  13. if else statement excel
  14. if else statement in r
  15. if else statement javascript
  16. if else statement example
  17. if else statement matlab
  18. if else statement in sql
  19. if else statement java exercises
  20. if else statement arduino