c# - asp.net 中的倒数计时器

标签 c# asp.net countdown

我正在使用带有 C# 的 asp.net 3.5。 我想创建一个倒数计时器,我的要求是这样的:

倒计时结束日期:2010 年 6 月 16 日 所以,直到 6 月 16 日到来之前,我的计时器将显示重新计算的时间。

请让我知道如何实现它,我用谷歌搜索了它,但没有得到解决我问题的 excat 解决方案。

提前致谢。

最佳答案

这是您需要使用 Javascript 解决的问题。您需要从服务器做的唯一一件事就是将结束日期设置为 Javascript 变量。您需要 Javascript,因为您只从服务器加载页面。之后您需要在客户端处理倒计时。

Javascript

<script type="text/javascript">
    function countdown_clock(clockID, year, month, day, hour, minute) {
        countdown(clockID, year, month, day, hour, minute);
    }

    function countdown(clockID, year, month, day, hour, minute) {
        Today = new Date();
        Todays_Year = Today.getFullYear();
        Todays_Month = Today.getMonth();

        //Convert both today's date and the target date into miliseconds.                           
        Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
                             Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
        Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();

        //Find their difference, and convert that into seconds.                  
        Time_Left = Math.round((Target_Date - Todays_Date) / 1000);

        if (Time_Left < 0)
            Time_Left = 0;

        days = Math.floor(Time_Left / (60 * 60 * 24));
        Time_Left %= (60 * 60 * 24);
        hours = Math.floor(Time_Left / (60 * 60));
        Time_Left %= (60 * 60);
        minutes = Math.floor(Time_Left / 60);
        Time_Left %= 60;
        seconds = Time_Left;

        dps = 's'; hps = 's'; mps = 's'; sps = 's';
        //ps is short for plural suffix.
        if (days == 1) dps = '';
        if (hours == 1) hps = '';
        if (minutes == 1) mps = '';
        if (seconds == 1) sps = '';

        var clock = document.getElementById(clockID);
        clock.innerHTML = days + ' day' + dps + ' ';
        clock.innerHTML += hours + ' hour' + hps + ' ';
        clock.innerHTML += minutes + ' minute' + mps + ' and ';
        clock.innerHTML += seconds + ' second' + sps;

        //Recursive call, keeps the clock ticking.
        setTimeout('countdown("' + clockID + '",' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ');', 1000);
    }
</script>

ASP.NET

protected override void OnPreRender(EventArgs e)
    {
        DateTime endDate = new DateTime(2010, 6, 1, 0, 0, 0);
        string script = string.Format("countdown_clock('clock', {0}, {1}, {2}, {3}, {4});", endDate.Year, endDate.Month, endDate.Day, endDate.Hour, endDate.Minute);
        ScriptManager.RegisterStartupScript(this, this.GetType(), "countdown", script, true);

        base.OnPreRender(e);
    }

为了示例目的从 My Little Scripts 中修改脚本.

关于c# - asp.net 中的倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2871725/

相关文章:

c# - 正则表达式不起作用并且超时

asp.net - 合并 Word 文档(Office Interop 和 .NET),保持格式

flutter - 日期倒数计时器

jquery - 在 WordPress 中使用 Jquery 倒计时

c# - GTK+ (GTKSharp) 在 Windows 中性能不佳

c# - 使用 Linq 查询解析 List<CustomClass> 中的 XML

c# - 如何在 url 中包含 Jquery UI 选项卡的 id?

xcode - 一段时间后更改 Xcode View

c# - 确保测试发现者和执行者已注册并且平台和框架版本设置合适,然后重试

jquery - 隐藏字段在回发时失去其值(value)