// Set the date we're counting down to const duration = seconds_left; var countDownDate = new Date().getTime() + duration * 1000; var x = setInterval(function() { var now = new Date().getTime(); var distance = countDownDate - now; // Time calculations for hours, minutes and seconds 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(document.getElementById("time") !== null && hours < 1 ){ document.getElementById("time").innerHTML = minutes + ":" + ((seconds < 10) ? "0" + seconds : seconds); //if seconds is lower then 10 add a 0 else just show seconds }else if(hours >= 1){ document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + ((seconds < 10) ? "0" + seconds : seconds); } if (distance <= 1 ){ clearInterval(x); timeExpired(); } }, 3); function timeExpired(){ window.location.replace("quiz.php"); unset($_SESSION['start_time']);// remove timer }