Problems with setInterval

3

Next, my problem is like this, I'm doing an inbox, so I need to update every 5 seconds of the messages. So far so good. In the div that loads the messages I put the following script:

function loadInbox1(id) {
 $("#load-inbox-1").html("<i class='fa fa-spin fa-cog'></i>");

 var timeout;
 clearInterval(timeout);

 setTimeout(function(){
      $('#load-inbox-1').load("/inbox-prof-load.php?from="+id);

        var timeout = setInterval(reloadChat, 5000);    
        function reloadChat () {
             $('#inbox-prof-load-msg').load('inbox-prof-load-msg.php?from='+id);
        }
 }, 500)

}

Where I'm printing user_id is the user I'm talking to. so it loads the chat every 5s in my div. Until then, calm down. but if I click on another window that loads on that same div another chat, the old script does not stop running and a new one also starts, causing the two conversations to appear quickly and is changing, if I click on more it does not stop or script and continue rolling several.

How can I do when I click on another chat to stop running with this old user_id and just run a script with the new one?

First page that the user chooses which inbox he wants to open

<div class="panel panel-default">
<div class="panel-heading">
    <h4 class="panel-title">
    Mensagens dos meus alunos
    </h4>
</div>

        <div class="panel-column panel-inbox no-margin">
            <div class="row no-margin">
                <div class="col-md-5 col-sm-3 col-xs-2 panel-sidebar">
                    <ul class="media-list no-margin" id="inbox-prof-alunos">

                        <?php do { ?>
                        <li class="media <?php if ($row_inbox['inbox_new'] != 0) { echo "active"; } ?>" href="javascript:func()" onclick="loadInbox1('<?=$row_inbox['user_id']?>')">
                            <div class="col-md-11 col-sm-11 no-padding">
                                <div class="media-left">
                                    <img class="media-object img-circle" src="/images/user/avatar/<?=utf8_encode($row_inbox['user_avatar'])?>" alt="Weeazy - <?=utf8_encode($row_inbox['user_nome'])?> <?=utf8_encode($row_inbox['user_sobre_nome'])?>">
                                </div>
                                <div class="media-body hidden-xs">
                                    <h4 class="media-heading"><?=utf8_encode($row_inbox['user_nome'])?> <?=utf8_encode($row_inbox['user_sobre_nome'])?></h4>
                                    <p class="inbox-preview"><?=substr(utf8_encode($row_inbox['user_inbox_msg']), 0, 35)?><?php if (strlen($row_inbox['user_inbox_msg']) > 35) { echo "..."; } ?></p>
                                </div>
                            </div>
                        </li>
                        <?php } while($row_inbox = mysql_fetch_assoc($Busca_inbox)); ?>

                    </ul>
                </div>

                <div class="col-md-7 col-md-offset-5 col-sm-9 col-sm-offset-3 col-xs-offset-2 panel-content">
                    <div class="inner" id="load-inbox-1">

                        <div class="media-header">
                            <div class="row">
                                <div class="col-md-12 media-header-meta">
                                    <span class="text-title"><i class="fa fa-comments"></i> Clique em uma conversa para expandir</span> 
                                </div>
                            </div>
                        </div>

                    </div>

                </div>
            </div>
        </div>


</div>

Script on this page:

function loadInbox1(id) {
 $("#load-inbox-1").html("<i class='fa fa-spin fa-cog'></i>");

 var timeout;
 clearInterval(timeout);

 setTimeout(function(){
      $('#load-inbox-1').load("/inbox-prof-load.php?from="+id);

        var timeout = setInterval(reloadChat, 5000);    
        function reloadChat () {
             $('#inbox-prof-load-msg').load('inbox-prof-load-msg.php?from='+id);
        } }, 500) }

Now the page that loads:

                            <div class="media-header">
                            <div class="row">
                                <div class="col-md-8 col-sm-9 col-xs-9 media-header-meta">
                                    <span class="text-title">Conversa com</span> 
                                    <ul class="list-inline no-margin">
                                        <li>
                                            <h4>
                                                <a href="/profile/aluno/<?=$row_inbox['user_url']?>"><?=$row_inbox['user_nome']?> <?=$row_inbox['user_sobre_nome']?></a>
                                            </h4>
                                        </li>
                                    </ul>
                                </div>

                            </div>
                        </div>
                        <div class="message-body">

                            <div id="inbox-prof-load-msg" class="message-content row nice-scroll">

                                <?php do { ?>

                                <div class="col-md-10" <?php if ($row_inbox['user_inbox_from'] == $user_id) { echo 'style="float:right;"'; } ?>>
                                    <span class="message-box message-<?php if ($row_inbox['user_inbox_from'] == $user_id) { echo 'mine'; } else { echo 'other'; }?>" data-toggle="tooltip" data-placement="<?php if ($row_inbox['user_inbox_from'] == $user_id) { echo 'left'; } else { echo 'right'; }?>" title="<?=date('H:i', strtotime($row_inbox['user_inbox_date']))?>">
                                    <?=nl2br(utf8_encode($row_inbox['user_inbox_msg']))?>
                                    </span>
                                </div>

                                <?php } while ($row_inbox = mysql_fetch_assoc($Busca_inbox)); ?>

                            </div>



                            <div id="reply" class="reply">
                                <form class="text-right" id="profile-add-inbox">
                                    <textarea class="form-control" name="msg" placeholder="Escreva sua mensagem aqui" style="height:80px;border-bottom: 1px solid #ddd;"></textarea> 
                                    <input type="hidden" name="to" value="<?=$other_id?>">
                                    <input type="hidden" name="aluno" value="<?=$other_id?>">

                                    <button type="submit" class="btn btn-warning">Enviar</button>
                                </form>
                            </div>
                        </div>

This page that is loaded that needs to be reloaded receiving as GET that from

    
asked by anonymous 16.12.2015 / 20:21

1 answer

2

The problem is that with each click you open a new chat window and start an ajax loop. If you click on people you will have 3 requests to run, all to change the content of the same div that is common.

I suggest you always have something to run in JavaScript (in the example it is function loader() { ), and each new chat window only changes the ID that is searched for.

 var pooler = (function() {
     var running = false;
     function loader() {
         if (!window.chatID) return;
         running = true;
         $('#inbox-prof-load-msg').load('inbox-prof-load-msg.php?from=' + window.chatID, function() {
             setTimeout(loader, 5000); // quando tiver carregado, pedir novo conteudo dentro de 5 segundos
         });
     }
     return function() {
         if (!running) loader(); // impedir multiplas execuções ao mesmo tempo
     }
 })();

 function loadInbox1(id) {
     window.chatID = id; // para saber qual a janela/chat aberta
     $("#load-inbox-1").html("<i class='fa fa-spin fa-cog'></i>");
     $('#load-inbox-1').load("/inbox-prof-load.php?from=" + id, function(){
        $('#inbox-prof-load-msg').html(''); // limpar a janela
        pooler(); // chamar o pooler
     });
 }
    
16.12.2015 / 20:29