Timer倒计时
程序员文章站
2022-06-08 22:32:06
...
Timer Timer = new Timer(1000);
public long lastTickTime = long.MinValue;
public long HeartTime = 10;
void Start () {
lastTickTime = GetTimeStamp();
TimerInit();
}
//倒计时
public void TimerInit()
{
Timer.Elapsed += new ElapsedEventHandler(HandleMainTimer);
Timer.AutoReset = false;
Timer.Enabled = true;
}
private void HandleMainTimer(object sender, ElapsedEventArgs e)
{
HearBeat();
Timer.Start();
}
private void OnDestroy()
{
Timer.Stop();
}
public void HearBeat()
{
long timeNow = GetTimeStamp();
long now = timeNow - lastTickTime;
Debug.Log(now);
if ((timeNow - lastTickTime) >= HeartTime)
{
Debug.Log("断线");
}
}
//获取时间点
public static long GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
上一篇: 自定义标签
下一篇: PHP正则表达式入门教程[转]