美文网首页
真实时间倒计时

真实时间倒计时

作者: RichMartin | 来源:发表于2021-05-11 17:01 被阅读0次
QQ截图20210511170042.png
using System;
using UnityEngine.UI;

public class CountDownTimer
{
    bool _timerIsRunning = false;
    Text timeText;
    DateTime daysLeft;
    Action action;

    public CountDownTimer(Text text,double day=0, double hour = 0, double min = 0, double sec = 0, Action ac = null)
    {
        daysLeft = DateTime.Now.AddDays(day).AddHours(hour).AddMinutes(min).AddSeconds(sec);
        action = ac;
        timeText = text;
    }
 
    public void TimerStart()
    {
        _timerIsRunning = true;
    }

    public void TimerStop()
    {
        _timerIsRunning = false;
    }

    public void Update()
    {
        if (_timerIsRunning)
        {
            DateTime startDate = DateTime.Now;
            var ts = daysLeft - startDate;
            if (ts < TimeSpan.Zero)
            {
                _timerIsRunning = false;
                action?.Invoke();
                return;
            }
            float days = ts.Days;
            float hour = ts.Hours;
            float minutes = ts.Minutes;
            float seconds = ts.Seconds;
            timeText.text = string.Format("{0}天{1:00}:{2:00}:{3:00}", days, hour, minutes, seconds);
        }
    }
}



    var timer = new CountDownTimer(text, day, hour, min, sec);
        timer.TimerStart();

相关文章

  • 真实时间倒计时

  • 短小精悍CountDownTimer

    一个短小精悍的冷门类时间倒计时,亦或者不止时间倒计时. CountDownTimer 用法: 即可实现60秒时间倒计时.

  • UIButton倒计时

    /** * 倒计时按钮 * * @paramtimeLine 倒计时总时间 * @paramtitle 还没倒计...

  • 倒计时

    倒计时 __block int timeout=10; //倒计时时间 // dispatc...

  • 倒计时、动态表格、防微博

    一、倒计时 倒计时 距离下课还有: || function task(){ //获取当前时间、 ...

  • 2018.5.24

    一、倒计时 倒计时 距离下课还有: || function task(){ //获取当前时间、 ...

  • 前端页面实现倒计时效果的几种方法

    1.15分钟倒计时 2.距离未来时间倒计时 3.数码时钟 4.时间插件倒计时:https://www.jq22.c...

  • js中date倒计时

    获取倒计时之前,我们不妨先来看怎样获取当前时间吧!!! 获取倒计时 1、实现倒计时功能2、倒计时结束按钮可被点击,...

  • vue时间倒计时

    后台返回了 一个创建时间需要根据这个时间 倒计时30分钟 然后时间就会倒计时了

  • JavaScript倒计时

    当前时间 显示倒计时

网友评论

      本文标题:真实时间倒计时

      本文链接:https://www.haomeiwen.com/subject/rtkndltx.html