Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text is displayed with delay

I want text I enter in a text field immediately to be shown in a div:

function func() {
  document.getElementById("query").innerHTML = document.getElementById("keyword").value;
}
window.onload = function() {
  keyword.onkeydown = function(e) {
    func();
  }
}
<input type="text" id="keyword" size="40">
<div id="query"></div>

It works, the only problem is, it is displayed with a delay.

So if I enter "abc", it only shows "ab". I need to enter another character, for example "abcd", so that it shows "abc".
The last character is always missing.

Here you can try it out: http://jsfiddle.net/285cz0np/

like image 616
Evgenij Reznik Avatar asked Nov 26 '15 22:11

Evgenij Reznik


People also ask

Why are my text messages delayed?

Delayed text messages is a real issue especially if the message is time sensitive. It can cause you a lot of trouble especially when these text messages are work-related and came from your superior. The problem of delayed text messages doesn’t actually seem to be caused by a specific messaging application.

How to fix typing delayed on Windows 10?

You may experience delays when typing if your PC’s keyboard driver is corrupt or incompatible with the keyboard. Fix this by uninstalling the driver; Windows will install a fresh copy afterward. 1. Launch the Device Manager, right-click the keyboard driver, and select Uninstall device. 2. Select Uninstall on the confirmation prompt to proceed. 3.

How can I test the sending functionality of a text message?

Aside from sending a message to your own number, you should also test its sending functionality by sending a message to that of another number beside yours. Sometimes the problem of delayed messages or error in messages happens at one point in time only.

How do I fix the delay on my external keyboard?

If you’re using a wired external keyboard, make sure the cable is tightly plugged into your PC’s USB port. Unplug the keyboard and plug it back into the port, or switch the keyboard to a different USB port. You could also use the keyboard on another computer. If the typing delay persists, the keyboard is probably faulty.


2 Answers

The event is being fired onkeydown. Since the value of the input element hasn't changed yet, you aren't seeing the results.

You could change it to onkeyup, instead: (updated example). Or you could set a delay before calling the function.


Alternatively, you could also listen to the input event:

Example Here

document.getElementById("keyword").addEventListener('input', function (e) {
    document.getElementById("query").textContent = e.target.value;
});
like image 195
Josh Crozier Avatar answered Oct 18 '22 06:10

Josh Crozier


The issue is because the input text value, not change until you release the key. by using setTimeout you let the browser to update the value, before running your function (func)

I recommend not to change onkeyup, becuase you want onkeydown, this solution will still work using the onkeydown event

keyword.onkeydown = function (e) {
        setTimeout(func,0)
    }

Update Example jsFiddle: http://jsfiddle.net/285cz0np/1/

I recommend you to change to input event. For example, if the user paste from the clipboard (by using only the mouse), your event will not fire.

like image 44
Aminadav Glickshtein Avatar answered Oct 18 '22 07:10

Aminadav Glickshtein