Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am developing a php application. but i cannot create a timer that the session can use for all the questions. i have seen some code but on refresh of the page, it starts counting again

What I have tried:

i have pretty done alot of research on what to do, for now everything seem abortive. the javascript will start counting again once the page is refreshed
Posted
Updated 12-Nov-21 22:59pm
Comments
Patrice T 17-Mar-16 18:38pm    
Show you code!
There so many tutorials on internet that it is unbelievable that you didn't found something working.

Based on Solution 1 but I fixed many errors

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language ="javascript" >
        var tim;
       
        var min = 0;
        var sec = 6;
        var f = new Date();
        function f1() {
            f2();
            document.getElementById("starttime").innerHTML = "Your started your Exam at " + f.getHours() + ":" + f.getMinutes();
             
          
        }
        function f2() {
        	debugger;
            if (parseInt(sec) > 0) {
                sec = parseInt(sec) - 1;
                document.getElementById("showtime").innerHTML = "Your Left Time  is "+ (min < 10 ? "0" + min : min) +":" + (sec < 10 ? "0" + sec : sec);
                tim = setTimeout("f2()", 1000);
            }
            else {
                if (parseInt(sec) == 0) {
                    min = parseInt(min) - 1;
                    if (parseInt(min) < 0) {
                        clearTimeout(tim);
                        document.getElementById("showtime").innerHTML = "Times up";
                        //location.href="/KB/answers/default5.aspx";
                    }
                    else {
                        sec = 59;
                        document.getElementById("showtime").innerHTML = "Your Left Time  is "+ (min < 10 ? "0" + min : min) +":" + (sec < 10 ? "0" + sec : sec);
                        tim = setTimeout("f2()", 1000);
                    }
                }
               
            }
        }
    </script>
</head>
<body onload="f1()" >
    <form id="form1" runat="server">
    <div>
      <table width="100%" align="center">
        <tr>
          <td colspan="2">
            <h2>This is head part for showing timer and all other details</h2>
          </td>
        </tr>
        <tr>
          <td>
            <div id="starttime"></div>
 
            <div id="endtime"></div>
 
            <div id="showtime"></div>
          </td>
        </tr>
        <tr>
          <td>
          </td>
        </tr>
      </table>
    </div>
    </form>
</body>
</html>
 
Share this answer
 
Comments
Richard Deeming 5-Oct-23 4:16am    
If you're going to post a copy of someone else's answer to "fix many errors", then you need to clearly explain what errors you've fixed, and how you've fixed them.

A quick comparison of the two shows that the only "errors" you've fixed are:

* Changing the initial time from 21 minutes to 6 seconds;
* Adding a debugger; statement which fires every second;
* Zero-padding the minutes and seconds;
* Changing the seconds from 60 to 59 when you reach a minute boundary;
* Changing the action that happens when the timer runs out;

Of that list, only the 60 vs 59 one truly counts as a "bug".
Your Code is Nice..Bt The exam timer stop before 1 min left to complete the examination..

I like to to change this section of code

if (parseInt(sec) == 0) {
min = parseInt(min) - 1;
if (parseInt(min) == 0)
{
clearTimeout(tim);
location.href="/KB/answers/default5.aspx";

}
Like below


if (parseInt(sec) == 0) {
min = parseInt(min) - 1;
if (parseInt(min) == -1)
{
clearTimeout(tim);
location.href="/KB/answers/default5.aspx";
}

I just change a single value is to get the expected output..That is,
if (parseInt(min) == -0) To if (parseInt(min) == -1)
 
Share this answer
 
what about 'setTimeout' method in javascript ? have you tried once ?
checkout below count down timer code snippet see if it works for you
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language ="javascript" >
        var tim;
       
        var min = 20;
        var sec = 60;
        var f = new Date();
        function f1() {
            f2();
            document.getElementById("starttime").innerHTML = "Your started your Exam at " + f.getHours() + ":" + f.getMinutes();
             
          
        }
        function f2() {
            if (parseInt(sec) > 0) {
                sec = parseInt(sec) - 1;
                document.getElementById("showtime").innerHTML = "Your Left Time  is :"+min+" Minutes ," + sec+" Seconds";
                tim = setTimeout("f2()", 1000);
            }
            else {
                if (parseInt(sec) == 0) {
                    min = parseInt(min) - 1;
                    if (parseInt(min) == 0) {
                        clearTimeout(tim);
                        location.href = "default5.aspx";
                    }
                    else {
                        sec = 60;
                        document.getElementById("showtime").innerHTML = "Your Left Time  is :" + min + " Minutes ," + sec + " Seconds";
                        tim = setTimeout("f2()", 1000);
                    }
                }
               
            }
        }
    </script>
</head>
<body onload="f1()" >
    <form id="form1" runat="server">
    <div>
      <table width="100%" align="center">
        <tr>
          <td colspan="2">
            <h2>This is head part for showing timer and all other details</h2>
          </td>
        </tr>
        <tr>
          <td>
            <div id="starttime"></div>
 
            <div id="endtime"></div>
 
            <div id="showtime"></div>
          </td>
        </tr>
        <tr>
          <td>
          </td>
        </tr>
      </table>
    </div>
    </form>
</body>
</html>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900