asp.net(VB)倒计时器,通过回发持久化

时间:2013-08-28 11:53:17

标签: javascript jquery asp.net vb.net timer

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

到目前为止,我已经尝试过使用Asp.Net计时器(使用Async触发器/更新面板),但是对于倒计时秒,这需要进行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

2 个答案:

答案 0 :(得分:1)

我的意思是,正如你所发现的那样,基本的问题是坚持它,虽然任何类型的回发都会变得困难而且不那么准确。您可以为内容页面使用iframe,并在外部页面上使用javascript运行计数器,还是可以在用户工作时启动计数器运行的新窗口?

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

答案 1 :(得分: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