I have found a tutorial online about timer and I have tried to modify it. I have added an window.location.href = "typage.php";
after the alert cause I have read that it can be used to redirect to a page when the ok button on the alert has been clicked.
The problem is when after clicking the ok button, it does not show the page that should be redirecting at. Instead, it still show the countdown timer and displays 'No records found'. Which is not correct, since I need to display the typage.php.
Also can I change the time format to mm:ss format?
<?php include ("inc/userheader.php");?>
<head>
<meta charset="utf-8">
<title>Quiz</title>
<script>
var mins = 1;
var secs = mins * 60;
function countdown() {
setTimeout('Decrement()', 60);
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
if (seconds < 59) {
seconds.value = secs;
}
else {
minutes.value = getminutes();
seconds.value = getseconds();
}
if (mins < 1) {
minutes.style.color = "red";
seconds.style.color = "red";
}
if (mins < 0) {
alert("Time's up!");
window.location.href = "typage.php";
minutes.value = 0;
seconds.value = 0;
}
else {
secs--;
setTimeout('Decrement()', 1000);
}
}
}
function getminutes() {
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
return secs - Math.round(mins * 60);
}
</script>
<body onload="countdown();">
<div class="container">
<h1> Quiz</h1>
<?php echo form_open('users/redirect/', ['class' => 'form-horizontal']);?>
<!-- onload function is evoke when page is load -->
<!--countdown function is called when page is loaded -->
<div>
<b>Time Left :
<input id="minutes" type="text" style="width: 10px;
border: none; font-size: 16px;
font-weight: bold; color: black;"><font size="5"> :
</font>
<input id="seconds" type="text" style="width: 20px;
border: none; font-size: 16px;
font-weight: bold; color: black;"></b>
</div>
<br>
<?php if(count($questions) > 0):?>
<?php $index = 1 ?>
<?php foreach($questions as $row):?>
<br>
<div class="row justify-content-center view">
<div class="col-lg-10">
<p><?=$index++?>. <b><?=$row->ques?></b></p>
<?php
$choices= array($row->ch_des1, $row->ch_des2, $row->ch_des3, $row->ch_des4);
shuffle($choices);
?>
<?php foreach($choices as $ch_des):?>
<input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$ch_des?>"> <?=$ch_des?><br>
<?php endforeach;?>
</div>
</div>
<?php endforeach;?>
<?php else:?>
<tr>
<td>No records found!</td>
</tr>
<?php endif;?>
</div>
</div>
<br><br>
<center><button type ="submit" class="btn btn-primary "> Submit </button></center>
</form>
</div>
</body>
<?php include ("inc/footer.php");?>