Create quiz timer or time countdown in PHP and Javascript.
<!DOCTYPE html>
<html lang="en">
<body>
<center>
<div class="time" id="navbar">Time left :<span id="timer"></span></div>
<button class="button" id="mybut" onclick="myFunction()">START QUIZ</button>
</center>
<div id="myDIV" style="padding: 10px 30px;">
<form action="result.php" method="post" id="form">
</form>
</div>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
var b = document.getElementById("mybut");
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
b.style.visibility = 'hidden';
x.style.display = "block";
startTimer();
}
}
window.onload = function() {
document.getElementById('myDIV').style.display = 'none';
};
</script>
<?php
$settime = 06 . ':' . 00;
?>
<script type="text/javascript">
document.getElementById('timer').innerHTML = '<?php echo $settime; ?>';
//06 + ":" + 00 ;
function startTimer() {
var presentTime = document.getElementById('timer').innerHTML;
var timeArray = presentTime.split(/[:]+/);
var m = timeArray[0];
var s = checkSecond((timeArray[1] - 1));
if(s==59){m=m-1}
document.getElementById('timer').innerHTML =
m + ":" + s;
if(m==0 && s==0){
//document.getElementById("form").submit();
console.log('Time is Done.');
if(s==0){
return true;
}
}
setTimeout(startTimer, 1000);
}
function checkSecond(sec) {
if (sec < 10 && sec >= 0) {sec = "0" + sec}; // add zero in front of numbers < 10
if (sec < 0) {sec = "59"};
return sec;
if(sec == 0 && m == 0){ alert('stop it')};
}
</script>
</body>
</html>
Comments
Post a Comment