Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need the code of timer which work continuous after reload the page.
normally after reload the page timer re started but I don't need to restart the timer.
To use the timer in online examination web page.
in asp.net or javascript
please help.
hurry
Posted
Updated 3-Mar-11 19:38pm
v2
Comments
Monjurul Habib 4-Mar-11 2:04am    
did you try anything? share your code.

Hi

As an example..

Have a javascript as below at the header of the page. if you are using master page then in the master page header section

XML
<script type="text/javascript" language="javascript">
     function displayRemainingTime() {
         var totalTime = parseInt(document.getElementById("totalTime").value);
         var elapsedTime = parseInt(document.getElementById("elapsedTime").value);
         var remainTime = totalTime - elapsedTime;
         document.getElementById("timerDisplay").innerHTML = "Time elapsed: " + elapsedTime + " minutes, " + remainTime + " minutes Remains";
         elapsedTime = elapsedTime + 1;
         document.getElementById("elapsedTime").value = elapsedTime;
         //alert(elapsedTime);
     }
 </script>


At the body on load event call this function <body onload="displayRemainingTime();"

The aspx page will have this controls..

XML
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:Label ID="timerDisplay" ClientIDMode="Static" runat="server" Text="Label"></asp:Label>
<input type="text" id="totalTime" clientidmode="Static" runat="server"  style="visibility:hidden"/>
<input type="text" id="elapsedTime" clientidmode="Static" runat="server"  style="visibility:hidden"/>
    <asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>



From your code behind of the aspx page at page_load event have this code..

XML
protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
         totalTime.Value = "30";// The exam total time in minutes.
         elapsedTime.Value = "0";
     }
     ClientScript.RegisterStartupScript(this.GetType(), "script1", "<script type='text/javascript'>setInterval('displayRemainingTime()',60000);</script>;");
 }




The workflow is

1) Assign the total time into a hidden text box.
2) javascript read it and calculate the remaining time. Set the elapsed time into a hidden text box
3) Both these textboxes will be having the values as such during post back. So the javascript can use that values
4)At page load we are injecting a interval script which is scheduled to run at one minute.

hope this helps
 
Share this answer
 
1. web.config file

XML
<configuration>
  <appsettings>
    <add key="sqlconnstr" value="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\My\Documents\testexam.mdf;Integrated Security=True;Connect Timeout=30" />
  </appsettings>
  <system.web>
    <compilation debug="true" targetframework="4.5" />
    <httpruntime targetframework="4.5" />
  </system.web>
</configuration>


2. default.aspx file

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function displayRemainingTime() {
            var totalTime = parseInt(document.getElementById("totalTime").value);
            var elapsedTime = parseInt(document.getElementById("elapsedTime").value);
            var remainTime = totalTime - elapsedTime;
            document.getElementById("timerDisplay").innerHTML = "Remaining Time :" + remainTime;
            elapsedTime = elapsedTime + 1;
            document.getElementById("elapsedTime").value = elapsedTime;
        }
    </script>
    <script type="text/javascript" src="count.js"></script>
</head>
<body onload="displayRemainingTime();">
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:Label ID="timerDisplay" ClientIDMode="Static" runat="server" Text="Label" Style="visibility: hidden"></asp:Label>
            <input type="text" id="totalTime" clientidmode="Static" runat="server" style="visibility: hidden" />
            <input type="text" id="elapsedTime" clientidmode="Static" runat="server" style="visibility: hidden" />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <fieldset>
                        <asp:Label ID="Label1" runat="server" Font-Size="Medium" ForeColor="DarkRed" Style="z-index: 100; position: absolute; top: 19px"
                            Text="Name : " Width="68px"></asp:Label>
                        <asp:TextBox ID="txtName" runat="server" Style="z-index: 101; left: 146px; position: absolute; top: 18px"></asp:TextBox>
                        <asp:Button ID="Button1" runat="server" Style="z-index: 102; left: 321px; position: absolute; top: 18px; right: 380px;"
                            Text="Start Exam" ToolTip="Enter Your Name" OnClick="Button1_Click" />
                        <asp:TextBox ID="txtScore" runat="server" Style="z-index: 103; left: 681px; position: absolute; top: 276px"
                            Visible="False" Width="63px">0</asp:TextBox>
                        <asp:Panel ID="Panel1" runat="server" BackColor="#E0E0E0" BorderColor="#E0E0E0" Height="264px"
                            Style="z-index: 104; left: 60px; position: absolute; top: 54px" Visible="False"
                            Width="707px" ForeColor="#0000C0">
                            <asp:Label ID="lblName" runat="server" Style="z-index: 100; left: 13px; position: absolute; top: 10px"
                                Text="Name : " ForeColor="#0000C0" Width="387px"></asp:Label>
                            <asp:Label ID="lblScore" runat="server" ForeColor="Green" Style="z-index: 102; left: 565px; position: absolute; top: 11px"
                                Text="Score : " Width="136px"></asp:Label>
                            <asp:Panel ID="Panel3" runat="server" Height="14px" Width="119px" Style="left: 427px; z-index: 106; position: absolute; top: 10px;">
                                <span id="cd" style="left: 100px;"></span>
                            </asp:Panel>
                            <asp:Panel ID="Panel2" runat="server" Height="214px" Style="z-index: 103; left: 8px; position: absolute; top: 41px"
                                Width="696px">
                                <asp:Label ID="lblQuestion" runat="server" Style="z-index: 100; left: 3px; position: absolute; top: 7px"
                                    Text="Label" Width="682px"></asp:Label>
                                <asp:RadioButtonList ID="RblOption" runat="server" Style="z-index: 102; left: 30px; position: absolute; top: 36px"
                                    Width="515px">
                                </asp:RadioButtonList>
                                <asp:Button ID="Button2" runat="server" Style="z-index: 106; left: 289px; position: absolute; top: 178px"
                                    Text="Next" ToolTip="Click Here to Take Next Question" OnClick="Button2_Click" />
                            </asp:Panel>
                            <asp:Label runat="server" ID="lblFinalScore" Style="z-index: 108; left: 289px; position: absolute; top: 170px"
                                Visible="False" />
                            <asp:Label runat="server" ID="lblCompleted" Style="z-index: 110; left: 289px; position: absolute; top: 200px"
                                Text="Test Completed Successfully" Visible="false" />
                        </asp:Panel>
                    </fieldset>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>
 
Share this answer
 
Comments
CHill60 16-Oct-15 10:32am    
Largely off-topic but you have copied the meat of the solution from Solution 2 - the accepted solution from over 4 years ago.
Take care to read the questions properly - OP asked for the code for a time, not an exam page nor connection to database

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