Timer accuracy

Last post 07-08-2009 4:07 AM by KulerMaster. 13 replies.

Sort Posts:

  • Timer accuracy

    06-27-2009, 7:00 AM
    • Member
      40 point Member
    • KulerMaster
    • Member since 11-16-2008, 8:31 PM
    • Posts 143

    I am using ajax toolkit and the timer for countdown function.

    It works fine except that it jumps over seconds ... although the interval of the timer is 1000 milliseconds.

    For example if it needs to count down from 60 to 0 it does that like following:

    60, 59, 57, 56, 54, 51, 50, 48 .... etc. etc.

    How do i fix this problem so the timer counts every second? Thanks

  • Re: Timer accuracy

    06-27-2009, 10:13 AM
    • All-Star
      92,340 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,527
    • Moderator
      TrustedFriends-MVPs

    It's probably a 'leaky boundary' condition.  The timer is doing:

    60000

    59000

    57999    // skips 58

    56999

    55990

     

    You could use your own int variable and decrement it 'about' every one second.

     

     

     

    Steve Wellens

    My blog
  • Re: Timer accuracy

    06-27-2009, 11:20 AM
    • Member
      40 point Member
    • KulerMaster
    • Member since 11-16-2008, 8:31 PM
    • Posts 143

    Thanks for the quick response.

    I must say it makes a perfect sense to me but, i am not sure about your suggestion of using own int var


    could you give an example? it will be much appreciated!

  • Re: Timer accuracy

    06-27-2009, 12:02 PM
    • All-Star
      92,340 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,527
    • Moderator
      TrustedFriends-MVPs

    There is more than one way.

    Are you using an Updatepanel and Server side functions to update the label?

    Or, are you doing it all at the client?

    Steve Wellens

    My blog
  • Re: Timer accuracy

    06-27-2009, 3:55 PM
    • Member
      40 point Member
    • KulerMaster
    • Member since 11-16-2008, 8:31 PM
    • Posts 143

    SGWellens:

    There is more than one way.

    Are you using an Updatepanel and Server side functions to update the label?

    Or, are you doing it all at the client?


    I am using UpdatePanel and Timer tick event to update the label controls


    Dim time As New TimeSpan()
    time = DirectCast(Session("Time"), DateTime) - DateTime.Now
    
    labelDays.Text = time.Days.ToString()
    labelHours.Text = time.Hours.ToString()
    labelMinutes.Text = time.Minutes.ToString()
    labelSeconds.Text = time.Seconds.ToString()


  • Re: Timer accuracy

    06-27-2009, 5:48 PM
    • All-Star
      92,340 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,527
    • Moderator
      TrustedFriends-MVPs

     You could use the techique below to get "even" counting but the clock will eventually drift off. 

    protected void UpdateCounter(object sender, EventArgs e)
    {
        DateTime MyTimer;
    
        //if (Session["Timer"] == null)
        //    MyTimer = DateTime.MinValue;
        //else
        //    MyTimer = (DateTime)Session["Timer"];
    
        // "Fancy" version of above 4 lines using null coalescing operator
        MyTimer = (DateTime)(Session["Timer"] ?? DateTime.MinValue);
    
        LabelCounter.Text = MyTimer.Second.ToString();
    
        MyTimer = MyTimer.AddSeconds(1);
    
        Session["Timer"] = MyTimer;
    }
    


     

     

    Steve Wellens

    My blog
  • Re: Timer accuracy

    06-28-2009, 3:41 AM
    • Member
      40 point Member
    • KulerMaster
    • Member since 11-16-2008, 8:31 PM
    • Posts 143

    Ok this really makes sense but how do i combine this with the TimeSpan anyway?

    I need to subtrack a DateTime.Now from the DateTime which is retrieved from DB.

    Pratically this is countdown ... Thanks!

  • Re: Timer accuracy

    06-28-2009, 9:40 AM
    • All-Star
      92,340 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,527
    • Moderator
      TrustedFriends-MVPs

     Here's another version:

    protected void UpdateCounter(object sender, EventArgs e)
    {
        TimeSpan MyTimer;
    
        if (Session["Timer"] == null)
            MyTimer = TimeSpan.FromSeconds(60);
        else
            MyTimer = (TimeSpan)Session["Timer"];
    
        LabelCounter.Text = MyTimer.Seconds.ToString();
    
        MyTimer = MyTimer.Subtract(TimeSpan.FromSeconds(1));
    
        Session["Timer"] = MyTimer;
    }
    


     

    Steve Wellens

    My blog
  • Re: Timer accuracy

    06-28-2009, 1:02 PM
    • Member
      40 point Member
    • KulerMaster
    • Member since 11-16-2008, 8:31 PM
    • Posts 143

    Hi Steve,

    I believe you expect me to figure out the rest of the code but, i must disappoint you.

    I am struglling with this for two days now and my brain is tottaly stuck ...

    This is the code that i am currently trying to work out so please if you can modify it to counts each second and update all FOUR labels. Thank you very much for any further help

    // Load event
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack) {
            DateTime NextUpdate = "06/30/2009 5:02:00 PM";
            Session("timer") = NextUpdate;
        }
    }
    
    // Timer's tick event
    protected void Timer1_Tick(object sender, System.EventArgs e)
    {
        TimeSpan timeSp = new TimeSpan();
        timeSp = (DateTime)Session("timer") - DateTime.Now;
        if (timeSp.Seconds <= 0 & timeSp.Minutes <= 0 & timeSp.Hours <= 0) {
            labelDays.Text = "00";
            labelHours.Text = "00";
            labelMinutes.Text = "00";
            labelSeconds.Text = "00";
        }
        else {
            labelDays.Text = timeSp.Days.ToString();
            labelHours.Text = timeSp.Hours.ToString();
            labelMinutes.Text = timeSp.Minutes.ToString();
            labelSeconds.Text = timeSp.Seconds.ToString();
        }
    }


  • Re: Timer accuracy

    06-28-2009, 10:50 PM
    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Timer accuracy

    06-29-2009, 1:14 PM
    • Member
      40 point Member
    • KulerMaster
    • Member since 11-16-2008, 8:31 PM
    • Posts 143

    chetan.sarode:


    Unfortunatelly it is not helpful in my sutuation. i'll wait for Steve's answer for a while

  • Re: Timer accuracy

    06-29-2009, 1:52 PM
    • All-Star
      92,340 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,527
    • Moderator
      TrustedFriends-MVPs

    KulerMaster:
    Unfortunatelly it is not helpful in my sutuation. i'll wait for Steve's answer for a while
     

    I believe you have all the information you need to figure it out. 

    Steve Wellens

    My blog
  • Re: Timer accuracy

    06-29-2009, 3:27 PM
    • Member
      40 point Member
    • KulerMaster
    • Member since 11-16-2008, 8:31 PM
    • Posts 143

    KulerMaster:

    Hi Steve,

    I believe you expect me to figure out the rest of the code but, i must disappoint you.

    I am struglling with this for two days now and my brain is tottaly stuck ...

    This is the code that i am currently trying to work out so please if you can modify it to counts each second and update all FOUR labels. Thank you very much for any further help

     

    1. // Load event  
    2. protected void Page_Load(object sender, System.EventArgs e)  
    3. {  
    4.     if (!IsPostBack) {  
    5.         DateTime NextUpdate = "06/30/2009 5:02:00 PM";  
    6.         Session("timer") = NextUpdate;  
    7.     }  
    8. }  
    9.   
    10. // Timer's tick event  
    11. protected void Timer1_Tick(object sender, System.EventArgs e)  
    12. {  
    13.     TimeSpan timeSp = new TimeSpan();  
    14.     timeSp = (DateTime)Session("timer") - DateTime.Now;  
    15.     if (timeSp.Seconds <= 0 & timeSp.Minutes <= 0 & timeSp.Hours <= 0) {  
    16.         labelDays.Text = "00";  
    17.         labelHours.Text = "00";  
    18.         labelMinutes.Text = "00";  
    19.         labelSeconds.Text = "00";  
    20.     }  
    21.     else {  
    22.         labelDays.Text = timeSp.Days.ToString();  
    23.         labelHours.Text = timeSp.Hours.ToString();  
    24.         labelMinutes.Text = timeSp.Minutes.ToString();  
    25.         labelSeconds.Text = timeSp.Seconds.ToString();  
    26.     }  
    27. }  
    // Load event
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack) {
            DateTime NextUpdate = "06/30/2009 5:02:00 PM";
            Session("timer") = NextUpdate;
        }
    }
    
    // Timer's tick event
    protected void Timer1_Tick(object sender, System.EventArgs e)
    {
        TimeSpan timeSp = new TimeSpan();
        timeSp = (DateTime)Session("timer") - DateTime.Now;
        if (timeSp.Seconds <= 0 & timeSp.Minutes <= 0 & timeSp.Hours <= 0) {
            labelDays.Text = "00";
            labelHours.Text = "00";
            labelMinutes.Text = "00";
            labelSeconds.Text = "00";
        }
        else {
            labelDays.Text = timeSp.Days.ToString();
            labelHours.Text = timeSp.Hours.ToString();
            labelMinutes.Text = timeSp.Minutes.ToString();
            labelSeconds.Text = timeSp.Seconds.ToString();
        }
    }




    Well as mentioned i am struglling with this issue for 3 days and i can't think of anything more.

    Now it's up to you. You can either modify the given code or just ignore it but, i will be thankful anyway.

  • Re: Timer accuracy

    07-08-2009, 4:07 AM
    • Member
      40 point Member
    • KulerMaster
    • Member since 11-16-2008, 8:31 PM
    • Posts 143

    I am sorry to say this but, it seems that you either do not understand my problem or you also don't know the solution.

    Thanks for the willing to help anwyway!

Page 1 of 1 (14 items)