Search code examples
asp.netvb.nettimercountdown

How to implement count down timer in asp.net using vb


Default.aspx

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="Label1" runat="server">2:00:00</asp:Label>
        <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
        </asp:Timer>
    </ContentTemplate>
</asp:UpdatePanel>

Default.aspx.vb

Dim timer, timer_arr() As String
timer = Label1.Text
timer_arr = timer.Split(":")
Dim seconds = Integer.Parse(timer_arr(2))
Dim minutes = Integer.Parse(timer_arr(1))
Dim hours = Integer.Parse(timer_arr(0))
If seconds = 0 And minutes = 0 And hours = 0 Then
    Timer1.Enabled = False
Else
    seconds = seconds - 1
End If
If (seconds < 0) Then
    seconds = 59
    minutes = minutes - 1
End If
If minutes < 0 Then
    minutes = 59
    hours = hours - 1
End If

Label1.Text = hours & ":" & minutes & ":" & seconds

The above code showing the expected result, i mean, i need the count down timer, which will display the countdown timer to the client like 2:45:34. But the problem is the value of seconds is decreasing by twice. i mean after 2:45:34 in the next second instead of displaying 2:45:33, it displaying 2:45:32 and so on. I'm very confused, from my point of view, everything is all right. Need help !!

UPDATED

I believe, client side scripting i.e. javascript will be the another option, but i need the timer for online exam system, which means timer should continue ticking whenever client send the request to the server. But, if i will use javascript then timer value will interrupted and will show unexpected value i.e. may be initialized again, and other cause. So, i believe server side scripting will be the best. If anyone have other suggestion, then i will appreciate. !!


Solution

  • I know, its too late to share answer of this question, Since, i found this solution and i felt, other can get the benefit of my solution.

    this is what i done...

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:Label ID="questionText" runat="server"></asp:Label>
    <asp:HiddenField ID="txt_timer" Value="00:30:00" runat="server" />
    <asp:Timer ID="Timer1" runat="server" Interval="1830" OnTick="Timer1_Tick"></asp:Timer>
    </ContentTemplate>
    </asp:UpdatePanel>
    

    I have used hiddenfield, which keep update the timer and now, i'm using <div> on which i'm displaying the counter

    <div id="counter" style="position: absolute; top: 165px; left: 850px;"></div> 
    

    now i added my javascript and css code to my file for better look of count-down timer

    timer.js

    <script src="../ajax/jquery.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../js/jquery.countdown.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript">
          $(function(){
            $('#counter').countdown({
              image: '../images/digits.png',
              startTime: document.getElementById('<%= txt_timer.ClientID %>').value,
            //  timerEnd: function(){ //alert('end!'); },        
               });           
          });
        </script>
    

    timer.CSS

    <style type="text/css">
        .cntSeparator
        {
            font-size: 55px;
            margin: 5px 2px;
            color: #000;
        }
    </style>
    

    You can see demo of countdown timer here