I need this for a chat box. When the user press enter, I want to trigger a callback (to send the message and clean the input field)
So far I got this, but the typing becomes slow:
$('#chat_input').keydown(function(event) {
if(event.keyCode == '13') {
// do something
}
});
How would you optimize this?
UPDATE: It’s not a huge slow down, but I can feel it. I’m pretty sure this code is the responsible. I have always had this problem, so I thought it was about time to find a different way if possible.
No, your code are not the guilty one here. Since you are building a chat system, you must be running other scripts as well. But here is another way to look into the problem.
Make a mini form
Then catch the submit event of the form, which will be triggered on enter automatically by the browser