laponac84 Posted August 20, 2013 Share Posted August 20, 2013 Code: jQuery code $(document).ready(function(){ setInterval(checkChat, 3000); }); function new_message(){ var val = $(".chat_box").attr('id'); var id_reciver = val.substr(val.indexOf("_") + 1) $(".chat_box").bind('keydown', function(e){ alert(e.keyCode); e.preventDefault(); }); } html code <textarea class='chat_box' id='input_"+chatuser+"' placeholder='type message' onkeyup='new_message()'></textarea> Problem description (time line of problem):1) The first time when i press any key on the keyboard, i do not have alert(e.keyCode)2) The second time when i press any key on the keyboard, i have correct alert(e.keyCode)3) The third time when I press the button on the keyboard, i have correct alert(e.keyCode), but 2 time alert`s4) ..... i have correct alert(e.keyCode), but 3 time alert`s5) ... and so on ....Why this happens?Alert instead, there should be standing at the mysql_query("insert into.... posts for inserting message in database, but, this code will be insert message "N" times in row... and that is not ok.PS:You can see the message box on attached picture (i have 2 same message in row)(i try to upgrade chat box application, and i m try to be like chat on Facebook, everything that I had planned I've done, I've just not about sending this message -... i send messages, but the few times one and the same :S) Quote Link to comment https://forums.phpfreaks.com/topic/281388-repeating-jquery-alert/ Share on other sites More sharing options...
Adam Posted August 21, 2013 Share Posted August 21, 2013 You're binding the event on every call to new_message(). That means every time its called, there will be an additional listener triggering the alert(). Just move the bind into the document ready code, or add some logic so that the bind code is only called once. Quote Link to comment https://forums.phpfreaks.com/topic/281388-repeating-jquery-alert/#findComment-1446176 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.