0

基本上我很擅长 javascript,我想为我的项目提供一个更好的倒数计时器。目前我的计时器看起来像这样:

01:25:05

我希望它看起来像这样:

1小时25分5秒

但我也希望它聪明一点。因此,如果它只有 25 分钟,它会说:

25 分钟。


这是我当前的 javascript 代码:

function secondCountdown(){ 
    if(typeof(skip) == "undefined"){
        skip = "set";
    } else {
        var timeleft = document.getElementById("timeleft").innerHTML;
        var time_split = timeleft.split(":");

        if(parseInt(time_split[0]) < 10){
            time_split[0] = time_split[0].charAt(1);
        }

        if(parseInt(time_split[1]) < 10){
            time_split[1] = time_split[1].charAt(1);
        }

        if(parseInt(time_split[2]) < 10){
            time_split[2] = time_split[2].charAt(1);
        }

        seconds = parseInt(time_split[2]) + parseInt(time_split[1]*60) + parseInt(time_split[0]*3600);
        seconds -= 1;

        if(seconds > 0){
                var hours= Math.floor(seconds/3600);
                seconds %= 3600;
                var minutes = Math.floor(seconds/60);
                seconds %= 60;

                var timeleft = ((hours < 10) ? "0" : "") + hours + ":" + ((minutes < 10) ? "0" : "") + minutes + ":" + ((seconds < 10) ? "0" : "") + seconds;

            document.getElementById("timeleft").innerHTML = timeleft;
        } else {
            document.getElementById("timeleft").innerHTML = "";
            window.location='test.php?pageid=' + getURLParam("pageid");
            return false;
        }
  }

  setTimeout("secondCountdown()",1000);
}
window.onload = secondCountdown;

也许有人可以为我编辑它以使其输出我想要的?

编辑:

这是计时器的 PHP 端。

<span id="timeleft">
$master = $timer['gta_time'] - time();

                echo date( "00:i:s", $timer['gta_time'] - time() ); 


</span>
4

3 回答 3

0

好的,我现在做了这个,它可以工作(我在 Opera 中测试过):

Date.prototype.toTimeString = function()
{
    var toReturnTime = new Array();
    var times = [this.getHours(), this.getMinutes(), this.getSeconds()];
    var times_names = [' hour', ' minute', ' second'];
    var tmp = null;
    for (var i=0; i<times.length; i++)
    {
    tmp = times[i];
    if (tmp > 0)
    {
        toReturnTime.push(tmp + times_names[i] + (tmp == 1 ? '' : 's'));
        toReturnTime.push(', ');
    }
    }
    if (toReturnTime.length/2 >= 2)
    {
    toReturnTime.pop();
    tmp = toReturnTime.pop();
    toReturnTime.pop();
    toReturnTime.push(' and ' + tmp);
    }
    else
    toReturnTime.pop();
    return toReturnTime.join('');
}


var date, timeleft;

function secondCountdown()
{
    date.setSeconds(date.getSeconds() - 1);
    timeleft.innerHTML = date.toTimeString();
    if ((date.getHours() + date.getMinutes() + date.getSeconds()) > 0)
    setTimeout("secondCountdown()",1000);
    else
    timeleft.innerHTML = 'ITS OVER';
}

function init()
{
    timeleft = document.getElementById("timeleft");
    var time_split = timeleft.innerHTML.split(":");
    date = new Date(0, 0, 0, parseInt(time_split[0]), parseInt(time_split[1]), parseInt(time_split[2]));
    secondCountdown();
}
window.onload = init;
于 2012-02-28T00:11:36.527 回答
0

哦,刚刚看到我对此有点慢,但它似乎有效,所以无论如何我都会添加它。:)

function parseTime() {
    var timeLeftStr;
    var timeLeft = 0;

    timeLeftStr = document.getElementById("timeleft").innerHTML;

    timeLeftStr.replace(/(\d+):(\d+):(\d+)/, function () {
        for (var i = 1; i < arguments.length - 2; i++) {
            // Convert to ms
            timeLeft += arguments[i] * Math.pow(60, 3 - i) * 1000;
        }
    });

    countdown(new Date(timeLeft));
}

function countdown(timeLeft) {
    var hours = timeLeft.getUTCHours();
    var minutes = timeLeft.getUTCMinutes();
    var seconds = timeLeft.getUTCSeconds();

    if (timeLeft.valueOf() == 0) {
        document.getElementById("timeleft").innerHTML = "";
        window.location = 'test.php?pageid=' + getURLParam("pageid");
        return false;
    } else {
        document.getElementById("timeleft").innerHTML =
            (hours == 0 ? "" : hours + (hours == 1 ? " hour" : " hours")) +
            (minutes == 0 ? "" : (hours ? (seconds ? ", " : " and ") : " ") + minutes + (minutes == 1 ? " minute" : " minutes")) +
            (seconds == 0 ? "" : (hours || minutes ? " and " : "") + seconds + (seconds == 1 ? " second" : " seconds"));

        setTimeout(function () { countdown(new Date(timeLeft - 1000)); }, 1000);
    }
}

window.onload = parseTime;

编辑以删除skip.

编辑 2,以删除对正则表达式、添加和and支持的开始和结束检查。

编辑 3以使用getUTCHours等,使其适用于其他时区(哎呀)。

于 2012-02-28T00:19:58.693 回答
0

而不是这一行:

var timeleft = ((hours < 10) ? "0" : "") + hours + ":" + ((minutes < 10) ? "0" : "") + minutes + ":" + ((seconds < 10) ? "0" : "") + seconds;

对基本解决方案说这样的话:

var timeleft = (hours > 0 ? hours + " hours " : "")
             + (minutes > 0 ? minutes + " minutes " : "")
             + (seconds > 0 ? seconds + " seconds " : "");

或全部:

var timeleft = "";
if (hours > 0)
   timeleft += hours + (hours > 1 ? " hours" : " hour");
if (minutes > 0) {
   if (hours > 0)
       timeleft += seconds > 0 ? ", " : " and ";
   timeleft += minutes + (minutes > 1 ? " minutes" : " minute");
}
if (seconds > 0) {
   if (timeleft != "")
      timeleft += " and ";
   timeleft += seconds + (seconds > 1 ? " seconds" : " second");
}
于 2012-02-27T23:47:37.913 回答