倒数计时器的顺序相反 [英] Countdown timer in reverse order
本文介绍了倒数计时器的顺序相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个倒数计时器,其代码如下:
Hi,
I've created a countdown timer whose code looks like:
protected void Timer1_Tick(object sender, EventArgs e)
{
try
{
dt1 = dt1.AddSeconds(1);
Label4.Text = (TimeSpan.FromHours(2D) - TimeSpan.FromSeconds(dt1.Second)).ToString("hh\\:mm\\:ss");
}
}
但上述代码仅工作1分钟。以相反的顺序然后重复继续。是否有可能以相反的顺序创建计时器半小时。
请提供片段
非常感谢提前
我尝试过:
我尝试上面的代码,输出如01:59:59,01:59:58等等。
But the above code worked for only 1 min. in reverse order then repetition goes on again. Can it be possible to create timer in reverse order for 1/2 hours.
Please provide the snippet
Thanks a lot in advance
What I have tried:
The above code I tried and the output comes like 01:59:59, 01:59:58 and so on.
推荐答案
而不是使用DateTime和更改它,当你启动计时器时占用当前时间,并设置目标时间:
Instead of using a DateTime and changing it, take the current time when you start teh timer, and set a target time:
private DateTime target;
...
target = DateTime.Now.AddMinutes(30);
Timer1.Start();
然后,在您的Tick处理程序中,检查区别:
Then, in your Tick handler, check the difference:
void Timer1_Tick(object sender, EventArgs e)
{
TimeSpan diff = target - DateTime.Now;
if (diff.TotalSeconds <= 0)
{
myTextBox.Text = "Done!";
Timer1.Stop();
}
else
{
myTextBox.Text = ((int) diff.TotalSeconds).ToString();
}
}
这样,它几乎可以用于任何偏移,并且更准确。
That way, it will work with almost any offset, and be a lot more accurate.
这篇关于倒数计时器的顺序相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文