1

我在我的 asp.net vb 页面上有一个按钮和一个标签,当按下按钮时,我需要一个倒数计时器,从 10 分钟开始,每秒更新一次,09:59、09:58、09:57 等... . 尽管有任何回发或临时重定向到不同的页面,我仍需要计时器继续倒计时。

到目前为止,我已经尝试使用 Asp.Net 计时器(带有异步触发器/更新面板),但是要倒计时几秒钟,这需要进行 1 秒的回发,并且当有数百名用户访问系统时,这将是相当昂贵的资源。它目前将确切的倒计时时间写入会话变量,然后在重新加载页面时检索该变量 - 这是一种非常丑陋的方法,但它们只有这样我才能让它工作。

更新 - 我找到了解决问题的方法:

使用这个很棒的 Javascript 倒计时脚本http://scripts.hashemian.com/js/countdown.js

ASPX 页面:

<asp:Literal runat="server" ID="jsTimeMarker"/>  
    <script type="text/javascript" src="http://scripts.hashemian.com/js/countdown.jss"></script>
    <asp:Button runat="server" Text="10 Mins" ID="btn10Mins"/>

页面背后的代码:

Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ' Set Countdown Timer based on if Cookie is set
        If Not Request.Cookies("CountDownTimer") Is Nothing Then
            TimeMarkerRender(Request.Cookies("CountDownTimer")("Data"))
        End If
    End If
End Sub

Sub TimeMarkerRender(TargetDateTime As String)
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("<script type=""text/javascript"">")
    'sb.Append("TargetDate = ""08/28/2013 18:11:29"";")
    sb.Append("TargetDate = " & "'" & TargetDateTime & "'" & ";")
    sb.Append("CountActive = true;")
    sb.Append("CountStepper = -1;")
    sb.Append("LeadingZero = true;")
    sb.Append("DisplayFormat = ""%%M%%:%%S%%"";")
    sb.Append("FinishMessage = ""Finished"";")
    sb.Append("</script> ")
    jsTimeMarker.Text = sb.ToString()
End Sub

Protected Sub btn10Mins_Click(sender As Object, e As EventArgs) Handles btn10Mins.Click
    Response.Cookies("CountDownTimer")("Data") = Now.AddMinutes(10).ToString("MM/dd/yyyy HH:mm:ss")
    Response.Redirect(Request.RawUrl)
End Sub
4

2 回答 2

1

更新 - 我找到了解决问题的方法:

使用这个很棒的 Javascript 倒计时脚本http://scripts.hashemian.com/js/countdown.js

ASPX 页面:

<asp:Literal runat="server" ID="jsTimeMarker"/>  
    <script type="text/javascript" src="http://scripts.hashemian.com/js/countdown.jss"></script>
    <asp:Button runat="server" Text="10 Mins" ID="btn10Mins"/>

页面背后的代码:

Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ' Set Countdown Timer based on if Cookie is set
        If Not Request.Cookies("CountDownTimer") Is Nothing Then
            TimeMarkerRender(Request.Cookies("CountDownTimer")("Data"))
        End If
    End If
End Sub

Sub TimeMarkerRender(TargetDateTime As String)
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("<script type=""text/javascript"">")
    'sb.Append("TargetDate = ""08/28/2013 18:11:29"";")
    sb.Append("TargetDate = " & "'" & TargetDateTime & "'" & ";")
    sb.Append("CountActive = true;")
    sb.Append("CountStepper = -1;")
    sb.Append("LeadingZero = true;")
    sb.Append("DisplayFormat = ""%%M%%:%%S%%"";")
    sb.Append("FinishMessage = ""Finished"";")
    sb.Append("</script> ")
    jsTimeMarker.Text = sb.ToString()
End Sub

Protected Sub btn10Mins_Click(sender As Object, e As EventArgs) Handles btn10Mins.Click
    Response.Cookies("CountDownTimer")("Data") = Now.AddMinutes(10).ToString("MM/dd/yyyy HH:mm:ss")
    Response.Redirect(Request.RawUrl)
End Sub
于 2013-08-28T16:11:45.770 回答
1

我的意思是,正如您所发现的那样,基本问题是尽管任何形式的回发都将变得困难且不准确,但仍要坚持下去。您可以为您的内容页面使用 iframe 并让计数器在外部页面上以 javascript 运行,还是可以在用户工作时启动一个新窗口并运行计数器?

这是一个简单的 javascript 倒数计时器: https ://mindgrader.com/tutorials/1-how-to-create-a-simple-javascript-countdown-timer

于 2013-08-28T13:58:46.177 回答