1

我创建了一个自定义 Jquery 计时器。但我面临的小问题我不知道这对我不起作用。下面是我的代码。

  function show(Hos, mins, secds) {

        var hours = Hos;
        var minutes = mins;
        var seconds = secds;
        var dn = "AM";
        if (hours > 12) {
            dn = "PM"
            hours = hours - 12
        }
                    if (hours == 0)
                        hours = 12

        document.getElementById('<%= Label1.ClientID %>').innerHTML = hours + ":" + minutes + ":" + seconds + " " + dn
        if (parseInt(seconds) == 59) {
            seconds = 0;
            if (parseInt(minutes) == 59) {
                if (parseInt(hours) == 12) {
                    hours = 0;
                } else {
                    hours = parseInt(hours) + 1;
                }
            } else {

                minutes = parseInt(minutes) + 1;
            }
        } else {
            seconds = parseInt(seconds) + 1;

        }


        setTimeout("show('" + hours + "','" + minutes + "','" + seconds + "'" + " )", 1000)
    }

这段代码运行良好,我第一次使用 c# 从后面的代码中传递小时、分钟、秒。现在我的问题是,如果秒小于 9、分钟小于 9 和小时小于,我想添加“ 0 ” 9. 我尝试了以下技巧,但我不知道为什么它对我不起作用..

if (seconds <= 9) {
                seconds = '0' + parseInt(seconds);
            }

请帮助我..实际上当我尝试这个时会发生什么。它将 0 与秒数连接到 9,但随着 9 的到来,它会从 1 重新启动。这就是问题所在。

4

2 回答 2

1

seconds = '0' + parseInt(seconds)将进行字符串加法,因为'0', 是一个字符串。

当您在javascript中将字符串添加到数字时,该数字将附加到字符串中。

Remove the quotes for 0, then it will be treated like number.

P.S. I guess this is the problem you are trying to crack

于 2012-08-02T08:05:10.870 回答
0

i have resolve this problem using some trick that is as follow:

function show(Hos, mins, secds) {

        var hours = Hos;
        var minutes = mins;
        var seconds = parseInt(secds, 10);


        if (hours == 0)
            hours = 12

        if (parseInt(seconds, 10) < 10) {
            seconds = "0" + parseInt(seconds, 10);
        }
        if (parseInt(hours, 10) < 10) {
            hours= "0" + parseInt(hours, 10);
        }
        if (parseInt(minutes, 10) < 10) {
            minutes= "0" + parseInt(minutes, 10);
        }
        document.getElementById('<%= Label1.ClientID %>').innerHTML = hours + ":" + minutes + ":" + seconds + " "
        if (parseInt(seconds) == 59) {
            seconds = 0;
            if (parseInt(minutes, 10) == 59) {
                if (parseInt(hours, 10) == 24) {
                    hours = 0;
                } else {
                    hours = parseInt(hours, 10) + 1;
                }
            } else {

                minutes = parseInt(minutes, 10) + 1;
            }
        } else {
               seconds = parseInt(seconds, 10) + 1;
       }
        setTimeout("show('" + hours + "','" + minutes + "','" + seconds + "'" + " )", 1000)
    }
于 2012-08-02T09:15:58.777 回答