Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am developing the Online Exam Project... and I am taking Timer, when User can give the Test....

But when User clicks On any button on the page, then Page is refreshing and Timer are restarted.

I am using this jQuery for timer :-
HTML
<script type="text/javascript">
        //for timer...
        var mins
        var secs;
        function cd() {
            mins = 1 * m(document.getElementById("TxtTimer").value);
            secs = 0 + s(":01");
            redo();
        }

        function m(obj) {
            for (var i = 0; i < obj.length; i++) {
                if (obj.substring(i, i + 1) == ":")
                    break;
            }
            return (obj.substring(0, i));
        }

        function s(obj) {
            for (var i = 0; i < obj.length; i++) {
                if (obj.substring(i, i + 1) == ":")
                    break;
            }
            return (obj.substring(i + 1, obj.length));
        }

        function dis(mins, secs) {
            var disp;
            if (mins <= 9) {
                disp = " 0";
            } else {
                disp = " ";
            }
            disp += mins + ":";
            if (secs <= 9) {
                disp += "0" + secs;
            } else {
                disp += secs;
            }
            return (disp);
        }

        function redo() {
            secs--;
            if (secs == -1) {
                secs = 59;
                mins--;
            }
            document.getElementById("TxtTimer").value = dis(mins, secs);
            if ((mins == 0) && (secs == 0)) {
                window.alert("Time is up. Press OK to continue...");
                document.getElementById('<%=BtnFinish.ClientID%>').click();
            } else {
                document.getElementById("form1").value = setTimeout("redo()", 1000);
            }
        }

        function init() {
            cd();
        }
        window. önload = init;
    </script>

Design side code :-
ASP.NET
<asp:DataList ID="DataList3" runat="server">
    <ItemTemplate>
        <input id="TxtTimer" style="width: 100px;" class="timer" type="text" value='<%# Eval("Time") %>' name="TxtTimer" />
    </ItemTemplate>
</asp:DataList>



[Edit member="Tadit"]
Added pre tags.
Removed unrelated tags.
Added new Tags.
Correct spelling mistakes.
Corrected the Title and added the Description taken from previous Title.
[/Edit]
Posted
v4
Comments
Laiju k 6-May-14 6:33am    
Edit the question dont make Title Cases for all words in the question<br><br>
 <br><br>
you didnt told what you want

Use update panel around button and give asyncronouspostback trigger for that button
 
Share this answer
 
Hi Harnis,

1.When the user clicks on "Start Exam" button enter target time in the database.e.g. "2014-05-06 18:00:00.000"

2.Bind that datetime to a Label

3.Now generate the countdown timer from that DateTime(Label Text).

4.Count down timer wouldn't be changed even if the user refreshes the Page.


Hope this Help.
Revert back if you have any doubt.
 
Share this answer
 

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