<?php
session_start();
/* If you refresh the page
or
leave the page to browse and come back
then the timer will continue to count down until finished. */
// $minutes and $seconds are added together to get total time.
$minutes = 20; // Enter minutes
$seconds = 0; // Enter seconds
$time_limit = ($minutes * 60) + $seconds + 1; // Convert total time into seconds
if(!isset($_SESSION["start_time"])){$_SESSION["start_time"] = mktime(date(G),date(i),date(s),date(m),date(d),date(Y)) + $time_limit;} // Add $time_limit (total time) to start time. And store into session variable.
?>
<html>
<head>
</head>
<body>
<input id="txt" readonly>
<script>
var ct = setInterval("calculate_time()",100); // Start clock.
function calculate_time()
{
var end_time = "<?php echo $_SESSION["start_time"]; ?>"; // Get end time from session variable (total time in seconds).
var dt = new Date(); // Create date object.
var time_stamp = dt.getTime()/1000; // Get current minutes (converted to seconds).
var total_time = end_time - Math.round(time_stamp); // Subtract current seconds from total seconds to get seconds remaining.
var mins = Math.floor(total_time / 60); // Extract minutes from seconds remaining.
var secs = total_time - (mins * 60); // Extract remainder seconds if any.
if(secs < 10){secs = "0" + secs;} // Check if seconds are less than 10 and add a 0 in front.
document.getElementById("txt").value = mins + ":" + secs; // Display remaining minutes and seconds.
// Check for end of time, stop clock and display message.
if(mins <= 0)
{
if(secs <= 0 || mins < 0)
{
clearInterval(ct);
document.getElementById("txt").value = "0:00";
alert("The time is up.");
}
}
}
</script>
</body>
</html>
แต่พอผมลองเรียก function อีกตัวหนึ่งขึ้นมาเพื่อ หยุดทำสั่ง แต่ไม่ได้ผลช่วยดูที่ครับควรปรับแก้ตรงไหน
คำสั่ง แสดงผลเป็น 0.00 ตามด้วย The time stop แต่พอกด OK ออกไป ทำไมเวลายังกลับมาทำงานต่อจน หมดไปเองครับ
////////
function stop_time()
{
if(document.myform.onClick = true)
{
clearInterval(ct);
document.getElementById("txt").value = "0:00";
alert("The time stop.");
}
}
//////