Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am developing an online exam application so i used stop watch. But this stop watch is reset when page is reload or press F5 button. I don't want to reset stop watch. So please help me i am so in trouble.
My code is :



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
var msec = 0;
var sec = 0;
var min = 0;

function starttimer(){
var hdntime = document.getElementById("hdntime");
hdntime.innerHTML = min + ":" + sec + ":" + msec;
go = setTimeout("starttimer()", 1);
msec++;
if (msec == 100) {
msec = 0;
sec++;
}
if (sec == 60) {
sec = 0;
min++;
}
}
function resetwatch() {
window.location.reload();
}
</script>
</head>
<body>
<form id="form1" runat="server">

00:00:00


<input id="Button1" type="button" value="Start Watch" onclick="starttimer()"/>
<input id="Button2" runat="server" type="button" value="Stop Watch" onclick="stopwatch()" />
<input id="Button3" type="button" value="Reset Watch" onclick="resetwatch()" />


</form>
</body>
</html>
Posted
Comments
E.F. Nijboer 28-Jan-13 5:31am    
You cannot track this client side because it isn't tamper proof. You should store the start time of the user in a server session and use that. Your website shows just an indicator of the time left but when submitting the exam, you should check if the user is within the time limit (using the start time stored on the server)
Member 10033955 27-Jun-13 4:29am    
I want that for the php.
If you have any solution do mail me at narenbruce@hotmail.com

1 solution

try this code.....study it as demo....then try implement in your logic....what u have to do is save time in session variable.....
.aspx page as follow

XML
<asp:Panel ID="Panel1" runat="server" Height="303px">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick"
            Enabled="False">
        </asp:Timer>
        <br />

      <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>

            <ContentTemplate>
                            <asp:Button ID="Button3" runat="server" Text="Start" Width="213px"
                    onclick="Button3_Click" />

                            <br />
                            <br />

                <asp:Label ID="Label9" runat="server" Text="Remaining Time"></asp:Label>
                &nbsp;:
                <asp:Label ID="Label8" runat="server" Text=""></asp:Label>
                <br />
            </ContentTemplate>
        </asp:UpdatePanel>
        <br />
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
            <ContentTemplate>
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        )
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                <br />
                <hr />
                 <asp:RadioButton ID="RadioButton1" runat="server" GroupName="option1" />
        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="option1" />
        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <asp:RadioButton ID="RadioButton3" runat="server" GroupName="option1" />
        <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
        <asp:RadioButton ID="RadioButton4" runat="server" GroupName="option1" />
        <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            </ContentTemplate>
        </asp:UpdatePanel>

       <asp:Button ID="Button1" runat="server" Text="Next" Width="215px"
            onclick="Button1_Click" />

    </asp:Panel>





.cs file i.e code behind file.....

C#
protected void Button3_Click(object sender, EventArgs e)
    {
        Timer1.Enabled = true;
        Session["start_time"] = DateTime.Now.ToLongTimeString();
        //Session["end_time"] = DateTime.Now.AddMinutes(1).ToLongTimeString();
        Session["end_time"] = 15;
        Button3.Enabled = false;
        Session["question_counter"] = 0;
        Session["rightquestions"] = 0;
        Session["currentans"] = "";
        Button1_Click(null, null);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Session["end_time"] = (int.Parse(Session["end_time"].ToString()) - 1).ToString();


        if (Session["end_time"].ToString() == "0")
        {

            Timer1.Enabled = false;

        }

        Label8.Text = Session["end_time"].ToString();
    }



remember this is not complete code...it is demo...ur problem of refresh is have solution over here....on Next button click it is not refreshed....try it...and then apply ur logic for Next question...most importantly all online exam give warning message before start exam that "DO NOT REFRESH PAGE DURING EXAMINATION TIME"....hope it will help...
 
Share this answer
 
Comments
rahemani_1200 29-Jan-13 5:03am    
thanx Palavi

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