Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript function called twice, how to handle this?
    text
    copied!<p>My application is a LiveChat developed in C# and ASP.NET</p> <p>I have a Client timer which call the webservice Function RetrieveMessages() every second. Additionally, soon after the user send a message i'm call de same function RetrieveMessages().</p> <p>I have a variable that store the last message Id, so, every RetrieveMessage call retrieve only the messages that is unread.</p> <p>Sometimes the the client show the same message twice, like so:</p> <p>Ewerton (14:22:20) : Hello! Ewerton (14:22:20) : Hello!</p> <p>the duplicity occurs only in client, the DataBase table is ok, no duplications.</p> <p>I suspect tha the Timer, and the Send Message is executing a RetrieveMessage before the variable lastMessageId is updated.</p> <p>How can i synchronize the call of the RetrieveMessage() ?</p> <p>Here are some code to analise.</p> <pre><code> // ThisFunction is a callback that RetrieveMessage every time the user send's a message function SendMessageSucess(cdMsgEnviada) { //Carrega Mensagens Avalon.Services.ChatService.RetrieveMessages(CodChamado, IdLastMsg, RetrieveMessagesSucess); } </code></pre> <p>OnTImer Tick</p> <pre><code> // Every second, verify is exist new messages function timer_onTick() { //Carrega Mensagens Avalon.Services.ChatService.RetrieveMessages(CodChamado, IdLastMsg, RetrieveMessagesSucess); } </code></pre> <p>The RetrieveMessages function</p> <pre><code>function RetrieveMessagesSucess(result) { var myMsgs = new Array(); for (var i = 0; i &lt; result.length; i++) { var obj = eval('(' + result[i] + ')'); if (obj != null) myMsgs[i] = obj; } for (var j = 0; j &lt; myMsgs.length; j++) { if (myMsgs.length &gt; 0) { // Armazeno o codigo da ultima mensagem recebida IdLastMsg = myMsgs[myMsgs.length - 1].cd_chat_message; if (par) { var novoconteudo = "&lt;div style='background-color: #EFEFEF; padding: 10px;'&gt;" par = false; } else { var novoconteudo = "&lt;div style='padding: 10px;'&gt;" par = true; } if (myMsgs[j].origem_mensagem == 1) // Msg enviada pelo cliente novoconteudo = novoconteudo + "&lt;b&gt;" + myMsgs[j].solicitante + ": &lt;/b&gt;" else novoconteudo = novoconteudo + "&lt;b&gt;" + myMsgs[j].tecnico + ": &lt;/b&gt;" var objDate = eval(myMsgs[j].datahora.replace(/\/Date\((\d+)\)\//gi, "new Date($1)")); novoconteudo = novoconteudo + "(" + objDate.format("HH:MM:ss") + ") "; novoconteudo = novoconteudo + myMsgs[j].texto + "&lt;/div&gt;"; divChatHistory.append(novoconteudo); AutoScroll(); if (myMsgs[j].origem_mensagem == 2) // Msg enviada por um tecnico show_popAlert() } } // Verifico se o chat esta ativo IsChatInativo(); } </code></pre> <p>Any Ideas ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload