2020-06-09 23:47:50 +08:00
|
|
|
|
using System;
|
2020-07-05 00:08:33 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2020-06-09 23:47:50 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
public partial class CountDown : StatisticComponentBase<DateTime>
|
|
|
|
|
{
|
2020-07-05 00:08:33 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Format { get; set; } = "hh:mm:ss";
|
2020-06-09 23:47:50 +08:00
|
|
|
|
|
2020-07-05 00:08:33 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback OnFinish { get; set; }
|
2020-06-09 23:47:50 +08:00
|
|
|
|
|
|
|
|
|
private Timer _timer;
|
|
|
|
|
|
|
|
|
|
private const int REFRESH_INTERVAL = 1000 / 30;
|
|
|
|
|
|
|
|
|
|
private TimeSpan _countDown = TimeSpan.Zero;
|
2020-07-05 00:08:33 +08:00
|
|
|
|
|
2020-06-09 23:47:50 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
2020-09-20 11:45:58 +08:00
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
2020-06-09 23:47:50 +08:00
|
|
|
|
_countDown = Value - DateTime.Now;
|
|
|
|
|
_timer = new Timer(StartCountDownForTimeSpan);
|
|
|
|
|
_timer.Change(0, REFRESH_INTERVAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartCountDownForTimeSpan(object o)
|
|
|
|
|
{
|
|
|
|
|
_countDown = _countDown.Add(TimeSpan.FromMilliseconds(-REFRESH_INTERVAL));
|
|
|
|
|
if (_countDown.Ticks <= 0)
|
|
|
|
|
{
|
|
|
|
|
_countDown = TimeSpan.Zero;
|
|
|
|
|
_timer.Dispose();
|
2020-07-05 00:08:33 +08:00
|
|
|
|
if (OnFinish.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
OnFinish.InvokeAsync(o);
|
|
|
|
|
}
|
2020-06-09 23:47:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InvokeStateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|