2
私はオーディオを録音するプロジェクトのためにタイマーを作ろうとしていますが、作成中はこの問題に直面しました。setIntervalは止まっていません、なぜですか?なぜclearIntervalはsetIntervalを停止していませんか?
は、私は、次のコードを持っている:
/** Audio **/
var timerseconds = 0;
$('.audio-recorder-dialog-con').on('click', '#record', function() {
gotrecordval = document.getElementById("record").value;
//Crónometro
var timerseconds = setInterval(function() {
rseconds = parseInt(document.getElementById("r-seconds").value);
if (rseconds == 59) {
document.getElementById("r-seconds").value = "00";
}
rseconds = parseInt(document.getElementById("r-seconds").value);
rseconds += 1;
if (rseconds < 10) {
document.getElementById("r-seconds").value = ("00" + rseconds).substr(-2);
}
if (rseconds >= 10) {
document.getElementById("r-seconds").value = rseconds;
}
}, 1000);
//
if (gotrecordval == "Empezar a Grabar Audio") {
document.getElementById("record").value = "Detener/Subir";
}
if (gotrecordval == "Detener/Subir") {
document.getElementById("record").value = "Empezar a Grabar Audio";
$('.audio-recorder-dialog-con').fadeOut(500);
$(".contenido-dialog-new-d").fadeIn(500);
$("#aviaudio").fadeIn(500);
clearInterval(timerseconds);
}
});
--FIXED--
私がしたsetIntervalの内側にこれを追加することによって、それを修正しました:
//Crónometro
var timerseconds = setInterval(function(){
rseconds = parseInt(document.getElementById("r-seconds").value);
if(rseconds==59){document.getElementById("r-seconds").value = "00";}
rseconds = parseInt(document.getElementById("r-seconds").value);
rseconds+=1;
if(rseconds<10){document.getElementById("r-seconds").value = ("00" + rseconds).substr(-2);}
if(rseconds>=10){document.getElementById("r-seconds").value = rseconds;}
--Code added-
$('html, body').on('click', '.open-audio', function(){
clearInterval(timerseconds);
});
--
}, 1000);
//
」。 open-audio "はユーザーの録画ダイアログを開くイメージなので、再度開くとclearIntervalが機能します。
'clearInterval'は' if'文の条件の背後にあるので、それをチェックしたいでしょう。それを超えると、コードをインデントすることで、読みやすくデバッグするのがずっと簡単になります。 – spanky
間隔がワンクリックで始まり、次のクリックでそれを止めるつもりならば、新しい間隔で 'timerseconds'を上書きしてしまい、古いインターバルへの参照が失われてしまい、今は止めることはできません。 – adeneo