01 October 2014

Sometimes we need to get the input’s value when the keydown event is triggered.

But we will see the text we type and the result we get is not displayed immediately.

So we can add setTimeout or defer function to postpone the function to the last of current call stack. Then we can get a consistent result with what we type in the input real time. The value of the input will be already modified with the type key at the time the function is called.

Here’s a simple example http://codepen.io/Oliverl/pen/HwhIp

See the Pen HwhIp by Oliver - Frontend Developer (@Oliverl) on CodePen.