asp.net或javascript中的计时器代码 [英] code for timer in asp.net or javascript

查看:67
本文介绍了asp.net或javascript中的计时器代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在重新加载页面后连续工作的计时器代码。

通常在重新加载页面计时器后重启但我不需要重新启动计时器。

在网上考试网页上使用计时器。

在asp.net或javascript

请帮忙。

hurry

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

推荐答案





作为一个例子..



在页面标题处有如下的javascript。如果你正在使用母版页然后在母版页标题部分



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

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





在加载事件的主体上调用此函数< body onload = displayRemainingTime();



aspx页面将具有此控件..





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

The aspx page will have this controls..

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







来自page_load事件的aspx页面后面的代码有这个代码..






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

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









工作流程是



1)将总时间分配到隐藏文本框中。

2)javascript读取并计算剩余时间。将已用时间设置为隐藏文本框

3)这两个文本框在回发期间将具有这样的值。因此,javascript可以使用该值

4)在页面加载时,我们正在注入一个间隔脚本,该脚本计划在一分钟内运行。



希望这有帮助





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


1。 web.config文件



1. web.config file

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





2. default.aspx file

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


这篇关于asp.net或javascript中的计时器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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