jquery - Javascript keydown timing -


I am working on a very time-sensitive application which uses the key press for user input. As I am talking about Milliseconds, I went ahead and tried a version like this:

  start function () {//stim.style.display =" block "; Rt_start = New Date () GetTime (); Response_allowed = 1; } Function End () {var t = rt_end - rt_start; //stim.style.display = "none"; Log.innerHTML = t; I ++; If (i & lt; iterative) {setTimeout ('start ();', 1000); }} Var rt_start; Var rt_end; Vertation = 100; Var i = 0; Var response_allowed = 0; Var excited; Var log; $ (Document) .ready (function () {document.onkeydown = function (e) {if (response_allowed == 1) {rt_end = new date (). GetTime (); response_allowed = 0; end ();}}; Stim = document.getElementById ('stim'); log = document.getElementById ('log'); start ();});  
  & lt; Script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" & gt; & Lt; / Script & gt; & Lt; Div id = "log" & gt; & Lt; / Div & gt; & Lt; Img src = "https://www.gravatar.com/avatar/cfefd93404e6b0eb3cde02b4b6df4e2b?s=128&d=identicon&r=PG&f=1" id = "excited" />         
Modify the code to be displayed (repeat two lines), it is very slow to about 30ms.

Does anyone tell me in the direction of why this is really a matter and how likely to avoid this extra delay?

Thanks

It is not available (for browsers, which do not provide it Are).

This is a high-resolution timestamp (which is designed with precise measurements in mind) with navigation timing and web display APIs (for example) because of this search in the Mozilla Developer Network I can not share more than two links in a post)

To get a DOMHighResTimeStamp, quickly the way - do a lot like yours with var ts = new date (). GetTime () to get a timestamp for milliseconds regularly; is:

var ts = performance.now ();

As I mentioned above, take a look at the Web Display API on MDN. It would be very useful if your application is really time-sensitive.

Edit:

Regarding your snippet, I think that if you hold a key below, you are always limited to the resolution of the keydown event (which fire continuously , But not every millisecond). You can easily see this behavior if you press a character key down with the text editor (continuously) and check how many times the character is written per second, I think, an OS setting Is controlled by.

You are limited to the "flow" associated with setTimeout / setInterval You see settime time queues some for the execution after given delay , but It does not guarantee execution on time, this means a "best effort" scenario and, if the browser is busy doing something, then it will be largely restored meaning: if you post a response after a second Use the setTimeout to re-enable the> code> variable, you can call it "approximate" (but not at all In) can expect to re-enable it after 1 second.


Comments