5 minute count down timer

Diamonddrake 1 Tallied Votes 3K Views Share

I needed a 5 minute countdown for a project I was working on so I hacked this up, It does nothing more and nothing less. Figured I would post it while I was here to save someone else the trouble. (Not that it's in anyway difficult to do.)

ddanbe commented: Nice! +6
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace CountDownTimers
{
    public class Fivemintimer
    {
        Timer T = new Timer();

        int Minutes = 5;
        int Seconds = 0;

        public Fivemintimer()
        {
            T.Interval = 1000;
            T.Tick += new EventHandler(T_Tick);
        }

        public delegate void onTimerFinishedDelegate();
        public event onTimerFinishedDelegate onTimerFinished;

        public delegate void onTimerChanged(FIVETimerEventArgs args);
        public event onTimerChanged onTimerTick;

        void T_Tick(object sender, EventArgs e)
        {
            if (Minutes == 0 && Seconds == 0)
            {
                T.Stop();

                if (onTimerFinished != null)
                {
                    onTimerFinished();
                }
            }
            else
            {

                if (Seconds == 0)
                {
                    Seconds = 59;

                    Minutes--;
                }
                else
                {
                    Seconds--;
                }

            }

            if (onTimerTick != null)
            {
                onTimerTick(new FIVETimerEventArgs(Minutes, Seconds));
            }

        }

        public void PlayPause()
        {
            if (T.Enabled)
            {
                T.Stop();
            }
            else
            {
                T.Start();
            }
        }

        public void Reset()
        {

            Minutes = 5;
            Seconds = 0;
        }

        public bool Enabled
        {
            get
            {
                return T.Enabled;
            }
        }


    }

    public class FIVETimerEventArgs : EventArgs
    {

        public string TimeLeft;

        public FIVETimerEventArgs(int min, int sec)
        {
            string S;
            if (sec < 10)
            {
                S = "0" + sec.ToString();
            }
            else
            {

                S = sec.ToString();
            }

            TimeLeft = min.ToString() + ":" + S;
        }

    }
}

Dani AI

Generated

Nice share, . If you want the 5-minute countdown to stay accurate under load or when the tab is backgrounded, avoid relying on a 1000 ms tick. Instead, always derive the display from the remaining time to a fixed end. That eliminates the drift noted and, by using a monotonic clock, avoids DST or manual clock changes.

<div id="timer">05:00</div>
<script>
(function () {
  var display = document.getElementById('timer');
  var duration = 5 * 60 * 1000;              // 5 minutes in ms
  var end = performance.now() + duration;    // monotonic, not wall clock
  function pad(n){ return n < 10 ? '0' + n : '' + n; }
  function tick(){
    var now = performance.now();
    var remaining = Math.max(0, end - now);
    var m = Math.floor(remaining / 60000);
    var s = Math.floor((remaining % 60000) / 1000);
    display.textContent = pad(m) + ':' + pad(s);
    if (remaining > 0){
      var next = 1000 - (Math.floor(now) % 1000); // align to next second
      setTimeout(tick, next);
    } else {
      alert('Time!');
    }
  }
  tick();
})();
</script>

For ASP.NET Web Forms, keep the timer entirely client-side and persist only the end timestamp. On first load, render a HiddenField with an ISO UTC end time (server: DateTime.UtcNow.AddMinutes(5)). On every load, compute end = performance.now() + (Date.parse(endUtc) - Date.now()). This keeps the countdown monotonic while surviving full or partial postbacks (rebind your init after UpdatePanel partial renders).

For extras, , add a sound or callback to make it an egg timer; and , the same pattern scales to work/break cycles without accumulating drift.

ddanbe 2,724 Professional Procrastinator Featured Poster

Nice! It is possible to turn this into an egg timer!

anders.blomqvist2 0 Newbie Poster

Looks like this is perfect for my needs. Anyone knows how to implement this in a webform application

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

For another timing idea, see my work and pause time timer in Python language. Maybe anybody want to do similar thing in C#? Or you could transfer it to Iron Python for .Net environment.

kplcjl 17 Junior Poster

You'd think 1000 would accurately represent 1 second. I found that if I compared ticks to the starting tick value that my elapsed time timer would occationally jump 2 seconds using 1000. By the time 5 minutes elapsed, you'd probably be off by about 5 seconds using this minute second process. On a timer that is dependent on human reactions, this code is fine, it'll be at most 5 seconds slow and if you use the minute/second method used here to display your time, you'd never have a clue your time is at all off.
(Since I didn't use UTCNow, my timer could jump an hour ahead or backwards. Wasn't thinking of anyone playing with code at 2AM on two special days a year when I wrote it and didn't set up the timer to display hours so you'd see 60+ minutes going forward.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.