秒表不应重置在页面卸载javascript [英] stop watch should not reset on page unload in javascript

查看:47
本文介绍了秒表不应重置在页面卸载javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在线考试申请,所以我用了秒表。但是当重新加载页面或按F5按钮时,此秒表会重置。我不想重置秒表。所以请帮助我,我遇到了麻烦。

我的代码是:







<%@ Page Language =C#AutoEventWireup =trueCodeFile =Default7.aspx.csInherits =Default7%>





<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://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 / javascriptlanguage =javascript>

var msec = 0;

var sec = 0;

var min = 0;



函数starttimer(){

var hdntime = document.getElementById(hdntime);

hdntime.innerHTML = min +:+ sec +:+ msec;

go = setTimeout(starttimer(),1);

msec ++;

if(msec == 100){

毫秒= 0;

秒++;

}

if(sec == 60){

sec = 0;

min ++;

}

}

function resetwatch(){

window.location.reload();

}

< / script>

< / head>

< body>

< form id =form1runat = server>

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 =Button1type =buttonvalue =Start Watchonclick =starttimer()/>

< input id = Button2runat =servertype =buttonvalue =Stop Watchonclick =stopwatch()/>

< input id =Button3type =buttonvalue =重置观察onclick =resetwatch()/>





<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>


</form>
</body>
</html>

推荐答案

试试此代码.....研究它作为演示......然后尝试在你的逻辑中实现....你必须要做什么o节省会话变量的时间.....

.aspx页面如下



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

<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文件i .e代码隐藏文件.....








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

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();
    }







记住这不是完整的代码...它是演示.. .ur刷新的问题是在这里有解决方案....在下一步按钮单击它不刷新....尝试...然后应用你的逻辑下一个问题...最重要的是所有在线考试给出警告信息在开始考试之前在考试时间内不要刷新页面....希望它会有所帮助......




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...


这篇关于秒表不应重置在页面卸载javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆