2017-08-25 5 views
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が機能します。

+4

'clearInterval'は' if'文の条件の背後にあるので、それをチェックしたいでしょう。それを超えると、コードをインデントすることで、読みやすくデバッグするのがずっと簡単になります。 – spanky

+0

間隔がワンクリックで始まり、次のクリックでそれを止めるつもりならば、新しい間隔で 'timerseconds'を上書きしてしまい、古いインターバルへの参照が失われてしまい、今は止めることはできません。 – adeneo

答えて

0

質問に追加した解決策は妥当ではありません。これにより、setIntervalタイマーごとに新しいイベントハンドラが作成されます。それはそれを行う正しい方法ではありません。

代わりに、唯一のあなたはそれを開始する必要がある場合にsetIntervalを実行し、その最初のifの内側にそれを置く:あなたは、その `var`宣言の` click`ハンドラに `timerseconds`スコープ

if (gotrecordval == "Empezar a Grabar Audio") { 
    //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); 
    // 
    document.getElementById("record").value = "Detener/Subir"; 
} 
関連する問題