Tutorial :Single character input with wake state


Back in college I wrote a game where the computer would sleep for 1 second, wake up and check to see if anything needed to be processed. Of course, if the user entered a single character command, it would respond immediately.

Is there a way to do that with JavaScript?


setTimeout() and setInterval() will allow you to execute some code at regular intervals.

You can also monitor key press events in the DOM. Libraries like jQuery make this really easy with built in support for keyDown, keyUp and keyPress events. You can see these here: http://docs.jquery.com/Events


function processKeyCommand(){    ...  }    document.onkeypress = processKeyCommand    function processEverythingEveryOneSecond(){    ...    setTimeout(function(){processEverythingEveryOneSecond()}, 1000)  }    processEverythingEveryOneSecond()  


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »