我創建了一個在線考試系統,並且在在線考試系統中添加了一個倒數計時器。以下是我的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代碼
請寫出如何計算的代碼小時,微秒,秒 – kishankakadiya
這應該是足夠的,親切驗證var hours = d.getHours(); // 01 var minutes = d.getMinutes(); // 20 var seconds = d.getSeconds(); – Ruhul
@ Ruhul so我的問題timelimit like(02:20:00)那麼如何計算小時,分鐘,秒 – kishankakadiya