2017-10-25 6 views
0

次のコードがあります。このタイマーの有効期限が切れると、期限切れのテキストの代わりに別のタイマーが開始されます。カウントダウンタイマーが期限切れになると、別のカウントダウンを開始します

var countDownDate = new Date("Oct 25, 2017 15:37:25").getTime(); 

var x = setInterval(function() { 

    var now = new Date().getTime(); 

    var distance = countDownDate - now; 
    var days = Math.floor(distance/(1000 * 60 * 60 * 24)); 
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
    var minutes = Math.floor((distance % (1000 * 60 * 60))/(1000 * 60)); 
    var seconds = Math.floor((distance % (1000 * 60))/1000); 

    document.getElementById("demo").innerHTML = days + " Days " + hours + " Hrs " 
    + minutes + " Min " + seconds + " Sec "; 

    if (distance < 0) { 
     clearInterval(x); 
     document.getElementById("demo").innerHTML = "T**imer Expired - instead of this another timer**"; 
    } 
}, 1000); 
+0

あなたが持っているもの**正確な**問題明確にしてくださいことはできますか?現時点では、あなたが知りたい/達成したいことを得るのは難しいです。 – cramopy

+0

特定の問題や疑問はありません。 – OptimusCrime

答えて

0

このコードはこのトリックを行う必要があります。カウンタが0になるたびに、最初にcountDownDateとNowの間にあった時間と同じ時間にカウントを再定義します。この量が可変である場合は、distanceToAdd


 
// Changed the value to reach for the demo 
 
var countDownDate = new Date().getTime() + 20000; 
 
    // Fixed Value to add each time the counter get to 0 
 
    var distanceToAdd = countDownDate - new Date().getTime(); 
 

 
var x = setInterval(function() { 
 

 
    var now = new Date().getTime(); 
 

 
    var distance = countDownDate - now; 
 
    var days = Math.floor(distance/(1000 * 60 * 60 * 24)); 
 
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
 
    var minutes = Math.floor((distance % (1000 * 60 * 60))/(1000 * 60)); 
 
    var seconds = Math.floor((distance % (1000 * 60))/1000); 
 

 
    if (distance < 0) { 
 
     //Add Time to your timer goal instead of canceling interval 
 
     countDownDate += distanceToAdd; 
 
    } 
 
    else { 
 
     document.getElementById("demo").innerHTML = days + "  Days " + hours + " Hrs " 
 
     + minutes + " Min " + seconds + " Sec "; 
 
    } 
 
}, 1000);
<html> 
 
\t <head> 
 
\t </head> 
 
\t <body> 
 
\t 
 
\t <div id="demo"></div> 
 
\t 
 
</body> 
 
</html>

0

の割り当てを変更する可能性がある関数を作成し、タイマーが終了するたびにそれを呼び出します。

$(document).ready(function() { 
 
    var i = 1 ; 
 
    setTimer(i) ; 
 
}) ; 
 

 

 
function setTimer(i) { 
 
    var countDownDate = new Date().getTime() + 3000 ; 
 

 
    var x = setInterval(function() { 
 
     var now = new Date().getTime() ; 
 

 
     var distance = countDownDate - now ; 
 

 
     if (distance < 0) { 
 
      clearInterval(x); 
 
      console.log("Timer " + i + " Finished. New Timer Stated!") ; 
 
      setTimer(i+1) ; 
 
     } 
 
     else { 
 
     console.log("Timer " + i + " Running") ; 
 
     } 
 
    }, 1000); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

関連する問題