locked
Implementing a count down timer when new data inserted into DB RRS feed

  • Question

  • User1669121061 posted

    Hi..In my asp web application I receive the data at a regular intervals of time, 5 mins. (If data inserted into DB at 10:20:00, next data at 10:25:00). I am showing count down timer which starts down counting from 5 mins(00:05:00). and a label which shows next data updating time as Next data updates at in a label (ex: if a data comes at 10:20:00 next data at 10:25:00). Now how do I show next data update time and a timer which down counts time from 5 mins simultaneously when new data inserted into SQL server.

    Monday, April 30, 2018 3:46 PM

All replies

  • User-1716253493 posted

    First, add inserteddate column to the table, set defaultvalue=getdate()

    user updatepanel trigered by timer control and a label

                <asp:Timer ID="Timer1" runat="server" Interval="1000">
                </asp:Timer>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="Timer1" />
                    </Triggers>
                </asp:UpdatePanel>
        Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim insertdate As DateTime 'use max(inserteddate) to set this value from db
            Dim nexttime As DateTime = insertdate.AddMinutes(5)
            Label1.Text = (nexttime - DateTime.Now).ToString()
        End Sub 

    Monday, April 30, 2018 7:03 PM
  • User1669121061 posted

    Isn't the next data updates Label will be executed each second and its dynamic. the label should show only 5 min (present time + interval(5 min)) unless and until next data inserted.When new data inserted then only it should be incremented 5 min with respect to present time. 

    Tuesday, May 1, 2018 8:20 AM
  • User-1716253493 posted

    Have you try it? You can manipulate what you want to display

    It's count down to next insert, it's display time left.

    Tuesday, May 1, 2018 8:57 AM
  • User-166373564 posted

    Hi veeran,

    Isn't the next data updates Label will be executed each second and its dynamic. the label should show only 5 min (present time + interval(5 min)) unless and until next data inserted.When new data inserted then only it should be incremented 5 min with respect to present time. 

    From my point of view, the next data updates label will just tell us the next time when data update, after the data refreshes you could set the next data update time. And, the countdown timer will display the left time when the data will be updated. 

    So, after you refresh the data, you could set the next data update time, and start the countdown timer. 

    For the countdown timer, you could also use JavaScript or JQuery to achieve it. Code as below:

            <div>
                <p id="demo"></p>
    
                <script>
                    // Set the date we're counting down to
                    var countDownDate = new Date();
                    countDownDate.setMinutes(countDownDate.getMinutes() + 5);
    
                    // Update the count down every 1 second
                    var x = setInterval(function () {
    
                        // Get todays date and time
                        var now = new Date().getTime();
    
                        // Find the distance between now an the count down date
                        var distance = countDownDate.getTime() - now;
    
                        // Time calculations for days, hours, minutes and seconds
                        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
                        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
                        var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
                        // Output the result in an element with id="demo"
                        document.getElementById("demo").innerHTML =  minutes + "m " + seconds + "s ";
    
                        // If the count down is over, write some text 
                        if (distance < 0) {
                            clearInterval(x);
                            document.getElementById("demo").innerHTML = "EXPIRED";
                        }
                    }, 1000);
                </script>
            </div>

    More details, you could refer to this article: https://www.w3schools.com/howto/howto_js_countdown.asp 

    Best regards,
    Angie

    Tuesday, May 1, 2018 9:04 AM