倒数计时器未完全正常工作

时间:2016-03-19 09:40:45

标签: asp.net vb.net time timer countdown

我的网站上有一个倒数计时器,每周星期日下午3点倒数,这是我们的新销售开始时间。当秒数达到30时,计时器似乎只会减少分钟,与我认为的小时数相同。以下是为我提供的代码,计时器可以在我的网页上看到.. http://www.onlinecarbooty.com/go-booting-front.aspx

... VB

    Dim seconds As Integer = DateDiff(DateInterval.Second, Date.Now, BootSale.nextDOW(DayOfWeek.Sunday))
    Dim days, hours, minutes As Integer
    days = seconds / 86400
    hours = (seconds Mod 86400) / 3600
    minutes = (seconds Mod 3600) / 60
    lblTimer.Text = days & " Day(s) " & hours & " Hour(s) " & minutes & " Minute(s) " ' & seconds Mod 60 & " Seconds"

    If Date.Now.DayOfWeek.ToString = "Sunday" And Date.Now.Hour >= 15 Then
        lblTimer.Text = "NEW SALE JUST GONE LIVE!"
    End If

Protected Sub timer1_tick(sender As Object, e As EventArgs)
    Dim seconds As Integer = DateDiff(DateInterval.Second, Date.Now, BootSale.nextDOW(DayOfWeek.Sunday))
    Dim days, hours, minutes As Integer
    days = seconds / 86400
    hours = (seconds Mod 86400) / 3600
    minutes = (seconds Mod 3600) / 60
    lblTimer.Text = days & " Day(s) " & hours & " Hour(s) " & minutes & " Minute(s) " & seconds Mod 60 & " Seconds"
End Sub

前端......

<asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"></asp:Timer>

<asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <h2 class="boldStyle">NEW Sale starts in <asp:Label ID="lblTimer" runat="server"></h2></asp:Label></h2>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="timer1" EventName="tick" />
    </Triggers>
</asp:UpdatePanel>

1 个答案:

答案 0 :(得分:0)

我希望直接提出另一种解决方案,而不是检查您提供的代码中的问题原因。

我建议您使用我的 TimeMeasurer 类来表示倒计时或计时器,所有操作都可以通过事件处理程序轻松指导,以通知时间变化。

https://github.com/ElektroStudios/ElektroKit/blob/6fbce42a8c59b0a06f679a369d19b7a1282023d0/Solution/v1.2/Elektro.DateAndTime/Types/TimeMeasurer.vb

使用此类倒计时的示例:

Private WithEvents countdown As New TimeMeasurer()

' Label used to display the remaining time.
Private lblCountdown As Label = Me.Label1

Private Shadows Sub Shown() Handles MyBase.Shown

    ' The time interval for the TimeUpdated event to raise.
    Me.countdown.UpdateInterval = 100 ' ms

    ' Run a countdown of 1 minute.
    Me.countdown.Start(TimeSpan.FromMinutes(1.0R))

End Sub

Private Sub Countdown_TimeUpdated(sender As Object, e As TimeMeasurerUpdatedEventArgs) _
Handles countdown.TimeUpdated

    ' Measure H:M:S:MS
    Me.lblCountdown.Text = 
       String.Format("{0:00}:{1:00}:{2:00}:{3:000}",
                     e.Remaining.Hours, e.Remaining.Minutes, 
                     e.Remaining.Seconds, e.Remaining.Milliseconds)

    If (e.Elapsed.Subtract(e.Remaining) = e.Goal) Then
        Me.lblCountdown.Text = "Countdown Done!"
    End If

End Sub

另一个例子可以在源代码文档中找到。

随意使用或根据需要进行调整。您可以自行检查源代码,避免您提到的问题。

我希望这可以帮到你!