Jump to content

Repeating jQuery alert


laponac84

Recommended Posts

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`s
4) ..... i have correct alert(e.keyCode)but 3 time alert`s
5) ... 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)


 

 

post-136675-0-32210100-1376997292_thumb.png

Link to comment
https://forums.phpfreaks.com/topic/281388-repeating-jquery-alert/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.