2016-08-20 90 views
-2

我創建了一個在線考試系統,並且在在線考試系統中添加了一個倒數計時器。以下是我的JavaScript代碼。 PLZ檢查如何使用javascript計算小時數,分鐘數,秒數

<h2><p style="float: right" id="countdown"></p></h2> 

<script> 
    $(document).ready(function() { 
$examination_test_id = $("#examination_test_id").val(); 
     $time_limit = $("#time_limit").val(); 
     var d = new Date($time_limit); //14-August-2016 01:20:00 
     var hours = d.getHours();  //01 
     var minute = d.getMinutes(); //20 
     var minutes = hours * 60 + minute; 
     var seconds = 60 * minutes;  //00 
     console.log(seconds); 
     if (typeof (Storage) !== "undefined") {  //checks if localStorage is enabled 
      if (sessionStorage.seconds) {      //checks if seconds are saved to localstorage 
       seconds = sessionStorage.seconds; 

      } 
     } 
     function secondPassed() { 
      var minutes = parseInt((seconds)/60); 
      var hours = parseInt(minutes/60); 
      var remainingSeconds = seconds % 60; 
      if (remainingSeconds < 10) { 
       remainingSeconds = "0" + remainingSeconds; 
      } 

      if (typeof (Storage) !== "undefined") { 
       sessionStorage.setItem("seconds", seconds); 
      } 

      document.getElementById('countdown').innerHTML = hours + ":" + minutes + ":" + remainingSeconds; 

      if (seconds == 0) { 
       clearInterval(myVar); 
       document.getElementById('countdown').innerHTML = alert('Timeout'); 
       window.location.href = base_url + "student/Examinations/check_answer/" + $examination_test_id; 

       if (typeof (Storage) !== "undefined") { 
        sessionStorage.removeItem("seconds"); 
       } 
      } else { 
       seconds--; 
      } 
     } 
     var myVar = setInterval(secondPassed, 1000); 

    }); 
}); 
</script> 

我的問題:coundown計時器應在1時20分零零秒開始,但對我來說,倒數計時器在01:80:00開始,爲什麼呢?請檢查我的javascript代碼

回答

0

var minutes = hours * 60 + minute;

1小時20分鐘總共80分鐘。你應該改變該行只是有var minutes = minute;

+0

請寫出如何計算的代碼小時,微秒,秒 – kishankakadiya

+0

這應該是足夠的,親切驗證var hours = d.getHours(); // 01 var minutes = d.getMinutes(); // 20 var seconds = d.getSeconds(); – Ruhul

+0

@ Ruhul so我的問題timelimit like(02:20:00)那麼如何計算小時,分鐘,秒 – kishankakadiya

0

在幾分鐘內,你的總走,

var minutes = hours * 60 + minute; 
    80= 1*60 + 20 
+0

所以如何計算小時,分鐘,秒,請寫出代碼 – kishankakadiya

相關問題