私のインターバルは停止しません。送信ボタンをクリックしてからインターバルを開始します。ボタンがクリックされたときにインターバルが停止しない
私はチャットシステムを作っているので、チャットを何度もリロードすることになっていますが、チャットのリロードが実行されているときにメッセージの送信が非常に遅いため、メッセージの送信中にチャットのリロードが中止され、後で再び開始します。
スクリプト:
<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>
'成功コールバックの内側myVar'は、外側' myVar'をシャドウイングされます。それらは2つの異なる変数です。 – elclanrs
どのように私はthayを修正するのですか? – TechTimeGames
@elclanrsは真実を伝えます。成功コールバックでvarキーワードを削除する –