2016-08-14 13 views
0

私のインターバルは停止しません。送信ボタンをクリックしてからインターバルを開始します。ボタンがクリックされたときにインターバルが停止しない

私はチャットシステムを作っているので、チャットを何度もリロードすることになっていますが、チャットのリロードが実行されているときにメッセージの送信が非常に遅いため、メッセージの送信中にチャットのリロードが中止され、後で再び開始します。

スクリプト:

<script> 
$(document).ready(function(){ 
    function auto_load(){ 
     $.ajax({ 
      url: "index.php?url=userchat1&id=1", 
      cache: false, 
      success: function(data){ 
      $("#chatdiv").html(data); 
      } 
     }); 
     } 
     auto_load(); 
     //Refresh auto_load() function after 500 milliseconds 
     var myVar = setInterval(function(){ auto_load() }, 500); 

     $('#beskedform').on('submit', function (e) {  
       e.preventDefault(); 
       $.ajax({ 
       type: 'post', 
       url: 'index.php?url=chatsubmit&id=1', 
       data: $('#beskedform').serialize(), 
       success: function() { 
        $("#msg").attr('value',''); 
        $("#msg").removeAttr('disabled'); 
        $("#msgbutton").removeAttr('disabled'); 
        var myVar = setInterval(function(){ auto_load() }, 500); 
       } 
      }); 
      clearInterval(myVar); 
      $("#msg").attr('disabled','disabled'); 
      $("#msgbutton").attr('disabled','disabled'); 

     }); 
    }); 
</script> 

HTML:

     <form id="beskedform"> 
          <textarea name="msg" id="msg" class="form-control content-group" rows="3" cols="1" placeholder="Skriv din besked..."></textarea> 

          <div class="row"> 
           <div class="col-xs-6"> 
           </div> 

           <div class="col-xs-6 text-right"> 
            <button type="submit" id="msgbutton" class="btn bg-teal-400 btn-labeled btn-labeled-right"><b><i class="icon-circle-right2"></i></b> Send</button> 
           </div> 
          </div> 
         </form> 
+3

'成功コールバックの内側myVar'は、外側' myVar'をシャドウイングされます。それらは2つの異なる変数です。 – elclanrs

+0

どのように私はthayを修正するのですか? – TechTimeGames

+2

@elclanrsは真実を伝えます。成功コールバックでvarキーワードを削除する –

答えて

0

、このスクリプトを使用してください。

<script> 
$(document).ready(function(){ 
function auto_load(){ 
    $.ajax({ 
     url: "index.php?url=userchat1&id=1", 
     cache: false, 
     success: function(data){ 
     $("#chatdiv").html(data); 
     } 
    }); 
    } 
    auto_load(); 
    //Refresh auto_load() function after 500 milliseconds 
    var myVar = setInterval(function(){ auto_load() }, 500); 

    $('#beskedform').on('submit', function (e) {  
      e.preventDefault(); 
      $.ajax({ 
      type: 'post', 
      url: 'index.php?url=chatsubmit&id=1', 
      data: $('#beskedform').serialize(), 
      success: function() { 
       $("#msg").attr('value',''); 
       $("#msg").removeAttr('disabled'); 
       $("#msgbutton").removeAttr('disabled'); 
       myVar = setInterval(function(){ auto_load() }, 500); 
      } 
     }); 
     clearInterval(myVar); 
     $("#msg").attr('disabled','disabled'); 
     $("#msgbutton").attr('disabled','disabled'); 

    }); 
}); 

+0

あなたが行った変更を見つけるのに3分かかっていました(上記のコメントを読む前に)...少なくともあなたがそれをした理由を説明する必要があります。 – blex

+0

私のスクリプトと投稿したスクリプトとの間に違いはありませんか? – TechTimeGames

+0

@TechTimeGames 'myVar = setInterval(function(){auto_load()}、500);' myVar'を再定義しません。 –

関連する問題