javascript - Typing notification in node.js using KeyUp -


i using node.js , making chat application.

assume user chatting user b. in chat applications whenever each user starts typing, notifier appears says " user x typing...".

i have "typing ..." functionality it. using method:

$(document).on('keyup','form.chat input:text', function(e){     var $id = $(this).attr('id'); // text id user id chating with.     if ( $(this).val().trim().length > 0 )         socket.emit("writingon", $id );     if ( $(this).val().trim().length === 0 )         socket.emit("writingoff", $id ); }); 

it working well. question is:
is method? because foreach keyup client sends req server

thanks in advance.

is method?

for sending every keystroke in live-edit application, yes. providing "is typing" feature, no.

you need send message when user starts typing, , message when hasn't typed predetermined period of time. use timeout reset on every following keystroke. in case, need collection of timeouts each field typing in.

var timeouts = {},     time = 2000; $(document).on('keyup','form.chat input:text', function(e){     var id = this.id/*,         isempty = /^\s*$/.test(this.value) */;     if (id in timeouts) // if scheduled         cleartimeout(timeouts[id]); // remove     else /* if (!isempty) */ // else first stroke         socket.emit("writingon", id);     // schedule sending off-message, canceled when     // keystroke happens     timeouts[id] = settimeout(function() {         socket.emit("writingoff", id);         delete timeouts[id];     }, /* isempty ? 0 : */ time); }); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -