2016-06-27 9 views
1

私は時間を完了した後にフォームを送信するカウントダウンタイマーを作成しようとしていますが、その間にユーザーがタイマーを停止すると残りの時間をカウントする必要があります。ここに私のコードです。カウントダウンタイマーからの残り時間はどうすればわかりますか?

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title></title> 
    <script type="text/javascript"> 
     var total_seconds = 600; 
     var c_minutes=parseInt(total_seconds/60); 
     var c_seconds=parseInt(total_seconds%60); 
     var hours=parseInt(total_seconds/3600); 
     function CheckTime(){ 
     document.getElementById('time-left').innerHTML='Time Left: '+hours+' hours '+c_minutes+' min '+c_seconds+' sec'; 
     if(total_seconds<=0){ 
      alert("oops no time left"); 
      setTimeout('document.form.submit()',1); 
     }else{ 
      total_seconds=total_seconds-1; 
      c_minutes=parseInt(total_seconds/60); 
      c_seconds=parseInt(total_seconds%60); 
      hours=parseInt(total_seconds/3600); 
      setTimeout("CheckTime()",1000); 
     } 
     } 
     var timeout = setTimeout("CheckTime()",1000); 
     setInterval(function() { 
     console.log('Time left: '+getTimeLeft(timeout)+'s'); 
     }, 2000); 
     function getTimeLeft(timeout) { 
     return Math.ceil((timeout._idleStart + timeout._idleTimeout - Date.now())/1000); 
     } 
    </script> 
    </head> 
    <body> 
    <h1>Countdown Timer</h1> 
    <div id="time-left"> </div> 
    <br/><br/> 
    <form method="post" name="form" action="timer.php"></form> 
    <button onclick="getTimeLeft()">Stop</button> 
    </body> 
</html> 
+0

このコードにはどのようなエラーがありますか? –

+0

ユーザーはどのようにタイマーを停止しますか? – Mairaj

+0

残りの時間を得ることができませんでした。実際に残りの時間を秒で取得したいのですが、このコードでは取得できませんでした。 –

答えて

0

あなたは、単にこれはsecondsにすでに存在すると、2秒ごとに更新した後のように秒の残り時間を取得するために、変数total_secondsを使用することができます。

var timeout = setTimeout("CheckTime()",1000); 
     setInterval(function() { 
     console.log('Time left: '+total_seconds+'s'); 
     }, 2000); 
1

ありがとうLeopardの私はそれを得ました。

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title></title> 
    <script type="text/javascript"> 
     var total_seconds = 600; 
     var c_minutes=parseInt(total_seconds/60); 
     var c_seconds=parseInt(total_seconds%60); 
     var hours=parseInt(total_seconds/3600); 
     function CheckTime(){ 
     document.getElementById('time-left').innerHTML='Time Left: '+hours+' hours '+c_minutes+' min '+c_seconds+' sec'; 
     if(total_seconds<=0){ 
      alert("oops no time left"); 
      setTimeout('document.form.submit()',1); 
     }else{ 
      total_seconds=total_seconds-1; 
      c_minutes=parseInt(total_seconds/60); 
      c_seconds=parseInt(total_seconds%60); 
      hours=parseInt(total_seconds/3600); 
      console.log(total_seconds); 
      setTimeout("CheckTime()",1000); 
     } 
     } 
     setTimeout("CheckTime()",1000); 

    </script> 
    </head> 
    <body> 
    <h1>Countdown Timer</h1> 
    <div id="time-left"> </div> 
    <br/><br/> 
    <form method="post" name="form"> 
     <input type="submit" name="sub" id="submt" value="Save"> 
    </form> 
    </body> 
</html> 
+0

ところで私の名前はMairaj Ahmadです – Mairaj

+0

ok nice co incident –

関連する問題