【问题标题】:ASP.NET countdown timer (hours, minutes, seconds)ASP.NET 倒数计时器(小时、分钟、秒)
【发布时间】:2021-09-26 13:56:15
【问题描述】:

我必须制作一个倒计时计时器,您可以在其中选择计时器倒计时的秒数、分钟数和小时数。我找到了一个视频 (https://youtu.be/zgxV7LaoGAk?t=930),这个人解释了一切是如何运作的,但他的计时器不包括小时数。

这是一个只有分钟和秒的倒数计时器的代码:

var currentMinutes = (newTime.Length > 2) ?
    int.Parse(newTime.Substring(0, newTime.Length - 2)) :
    0;

var currentSeconds = (newTime.Length > 2) ?
    int.Parse(newTime.Substring(newTime.Length - 2)) :
    int.Parse(newTime);

谁能告诉我如何编写带有秒、分和小时的倒数计时器的代码(var currentHours ?)

var currentHours = ...


var currentMinutes = (newTime.Length > 2) ?
    int.Parse(newTime.Substring(0, newTime.Length - 2)) :
    0;

var currentSeconds = (newTime.Length > 2) ?
    int.Parse(newTime.Substring(newTime.Length - 2)) :
    int.Parse(newTime);

【问题讨论】:

    标签: asp.net timer countdown countdowntimer


    【解决方案1】:

    好的,那该怎么做呢?

    好吧,我们可以编写 100% 的服务器端代码 - 如果你卡住了,那就太好了 - 不懂 JavaScript

    所以,假设我们放入 3 个标签

    放入 3 个文本框。

    加入一个计时器控件。

    放入 4 个按钮 - (稍后查看我们如何使用它们)。

    我们有这个:

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    
        <div style="width:350px;padding:20px">
    
        <div style="margin:auto">
    
            <div style="float:left">
                <asp:Label ID="lblHours" runat="server" Text="Hours" Font-Size="Larger" ClientIDMode="Static"></asp:Label>
                <br />
                <asp:TextBox ID="txtHours" runat="server" Text="0" ClientIDMode="Static" CssClass="tbox"></asp:TextBox>
            </div>
    
            <div style="float:left;margin-left:20px">
                <asp:Label ID="lblMin" runat="server" Text="Minutes" Font-Size="Larger"></asp:Label>
                <br />
                <asp:TextBox ID="txtMin" runat="server" Text="0" CssClass="tbox" ClientIDMode="Static"></asp:TextBox>
            </div>
    
            <div style="float:left;margin-left:15px">
                <asp:Label ID="lblsec" runat="server" Text="Seconds" Font-Size="Larger"></asp:Label>
                <br />
                <asp:TextBox ID="txtSec" runat="server" BorderStyle="Solid" Text="5" CssClass="tbox" ClientIDMode="Static" ></asp:TextBox>
            </div>
    
            <div style="clear:both;height:15px"></div>
    
            <div style="margin:auto">
                <asp:Button ID="cmdStart" runat="server" Text="Start" />
                <asp:Button ID="cmdStop" runat="server" Text="Stop" style="margin-left:20px" />
    
                <asp:Button ID="cmdStartJS" runat="server" Text="Start JS" style="margin-left:20px"
                    OnClientClick="mystart();return false" />
                <asp:Button ID="cmdStopJS" runat="server" Text="Stop JS" style="margin-left:20px"
                    OnClientClick="mystop();return false"/>
                <br />
                <asp:Label ID="lblDone" runat="server" Text="" ClientIDMode="Static"></asp:Label>
            </div>
    
        </div>
        </div>
        <asp:Timer ID="Timer1" runat="server"></asp:Timer>
    

    我们的代码将如下所示:

    Protected Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click
    
        ' take entered values - convert to time
        Dim MyTime As DateTime
        MyTime = TimeSerial(txtHours.Text, txtMin.Text, txtSec.Text)
    
        ' save time value
        ViewState("MyTime") = MyTime
    
        Timer1.Interval = 1000  ' upddate every one second
        Timer1.Enabled = True   ' start the timer.
        lblDone.Text = "Started"
    End Sub
    
    Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
        Dim myTime As DateTime = ViewState("MyTime")
    
        ' check if we are to stop
        If myTime <= TimeSerial(0, 0, 0) Then
            Timer1.Enabled = False      ' turn off timer
            ' do whatever for end of timer
            lblDone.Text = "Done!"
            Exit Sub
        End If
    
        Dim OneSecond As TimeSpan = TimeSpan.FromSeconds(1)
    
        myTime = myTime - OneSecond
        ' update text boxes
        txtHours.Text = myTime.Hour
        txtMin.Text = myTime.Minute
        txtSec.Text = myTime.Second
    
        ViewState("MyTime") = myTime
    
    End Sub
    
    Protected Sub cmdStop_Click(sender As Object, e As EventArgs) Handles cmdStop.Click
        Timer1.Enabled = False
    End Sub
    

    输出:

    所以上面说的桌面方法非常相似。 (就像在桌面上一样,我们甚至加入了计时器控件)。

    当然,缺点是这意味着每 1 秒回发一次。

    但是,我们可以在上面的标记周围加上一个更新面板——它会看起来很光滑。

    第二种方式:

    100% 客户端代码。

    所以,通过上面的页面,让我们为“js 按钮”编写 JavaScript 代码

    我们有这个(我确实假设这个页面使用了 jQuery - 但现在很多人都这么认为)。

    <script>
    
        MyTime = new Date(0,0,0,0,0,0)
        TimeZero = new Date(0,0,0,0,0,0)
        MyTimer = 0
    
        tH = $("#txtHours")
        tM = $("#txtMin")
        tS = $("#txtSec")
    
        function mystart() {
    
            MyTime.setHours(tH.val())
            MyTime.setMinutes(tM.val())
            MyTime.setSeconds(tS.val())
    
            if (MyTime > TimeZero) {
                MyTimer = setInterval(mytic,1000)
                $("#lblDone").text("Start")
            }
        }
    
        function mytic() {
    
            MyTime.setSeconds(MyTime.getSeconds() - 1)
    
            tH.val(MyTime.getHours())
            tM.val(MyTime.getMinutes())
            tS.val(MyTime.getSeconds())
    
            if (MyTime <= TimeZero) {
                clearInterval(MyTimer)
                $("#lblDone").text("Done !!!")
            }
        }
    
        function mystop() {
                clearInterval(MyTimer)
        }
    </script>
    

    因此,无论是 JavaScript 还是服务器端代码,都做同样的事情。

    您注意到的主要区别是客户端代码? 它永远不会到达服务器端,因此看起来更加流畅。

    【讨论】:

    • 我不想听起来粗鲁,但你没有回答我的问题
    • 我发布了倒计时计时器的两个工作版本,包括小时、分钟和秒。而且您没有指定是否需要 100% 的服务器端代码而不必编写 JavaScript。我的第一个例子就是这样做的。第二个示例是 100% 的客户端代码,同样带有倒数计时器,该示例还包括显示小时数。所以我不确定我在这里缺少什么?事实上,在任何一种情况下,我们看到真正巨大的技巧是使用日期/时间变量。这消除了编写/管理递增/递减小时、分钟、秒的需要,因此将节省大量代码
    • 我是一个初学者(这是我的第一个 ASP 项目),我只是想知道这段代码的含义和作用,以便我可以将小时数添加到倒数计时器( var currentMinutes = (newTime. Length > 2) ? int.Parse(newTime.Substring(0, newTime.Length - 2)) : 0; ) 我想试试你回复的第一个工作版本,但我不知道把第二部分放在哪里的代码(受保护的子...)。我假设代码的第一部分转到一个 .aspx 文件,但第二部分在哪里呢? - 我尝试了 aspx.cs,但没有奏效。谢谢!
    • 好的,我发布了 Web 表单的代码。不清楚您使用的是 c# 还是 vb.net(不是什么大问题),以及您创建了什么样的 web 项目。 (mvc 项目或网络表单)。 Web 表单现在已经很老了,但如果您刚开始,它们可能会很棒。如前所述,您可以选择将此解决方案编写为 100% 服务器端代码,或者您可以将代码编写为将在浏览器“内部”运行的 JavaScript。 (因此代码在用户桌面上运行,甚至不需要后面的代码)。至于代码去哪儿了?好吧,假设您至少可以将一个按钮放入标记中并让它调用服务器代码
    • 所以,您没有提供一些示例标记以及到目前为止您尝试的内容。因此,如果没有您尝试过的至少一些示例代码 + 标记,那么很难猜测哪种解决方案和建议对您有用,甚至是合适的。不清楚你是想避免使用 JavaScript,还是乐于使用 JavaScript。刚开始时,我倾向于坚持 100% 的服务器端代码(代码隐藏),因为它干净的 c# 或干净的 vb.net 代码。所以这是一个学习曲线的问题。随着时间的推移,毫无疑问,人们会采用和编写更多的 JavaScript,但从服务器端代码开始会更容易
    猜你喜欢
    • 1970-01-01
    • 2014-10-26
    • 2019-11-24
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多