Javascript 好的倒计时计时器

Javascript 好的倒计时计时器,javascript,countdowntimer,Javascript,Countdowntimer,基本上我擅长javascript,我希望我的项目有一个更好的倒计时计时器。此时,我的计时器看起来像这样: 01:25:05 我希望它看起来像这样: 1小时25分5秒 但我也希望它是聪明的。因此,如果只有25分钟,它会说: 25分钟 这是我当前的javascript代码: function secondCountdown(){ if(typeof(skip) == "undefined"){ skip = "set"; } else { var

基本上我擅长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;
函数secondCountdown(){
if(typeof(skip)=“未定义”){
skip=“set”;
}否则{
var timeleft=document.getElementById(“timeleft”).innerHTML;
var time_split=timeleft.split(“:”);
if(parseInt(时间分割[0])<10){
时间分割[0]=时间分割[0]。字符(1);
}
if(parseInt(时间分割[1])<10){
时间分割[1]=时间分割[1]。字符(1);
}
if(parseInt(时间分割[2])<10){
时间分割[2]=时间分割[2]。字符(1);
}
秒=parseInt(时间分割[2])+parseInt(时间分割[1]*60)+parseInt(时间分割[0]*3600);
秒-=1;
如果(秒>0){
var小时=数学地板(秒/3600);
秒%=3600;
var分钟=数学地板(秒/60);
秒%=60;
变量timeleft=((小时<10)?“0”:“)+hours+”:“+((分钟<10)?“0”:“)+minutes+”:“+((秒<10)?“0”:“)+seconds;
document.getElementById(“timeleft”).innerHTML=timeleft;
}否则{
document.getElementById(“timeleft”).innerHTML=“”;
window.location='test.php?pageid='+getURLParam(“pageid”);
返回false;
}
}
setTimeout(“secondCountdown()”,1000);
}
window.onload=第二次倒计时;
也许有人可以帮我编辑它,让它输出我想要的

编辑:

这是计时器的PHP端

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

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


</span>

$master=$timer['gta_time']-time();
回显日期(“00:i:s”,$timer['gta_time']-time());
代替此行:

var timeleft = ((hours < 10) ? "0" : "") + hours + ":" + ((minutes < 10) ? "0" : "") + minutes + ":" + ((seconds < 10) ? "0" : "") + 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");
}

好的,我现在做了这个,它成功了(我在Opera中测试过):

Date.prototype.toTimeString=函数()
{
var toReturnTime=新数组();
var times=[this.getHours()、this.getMinutes()、this.getSeconds()];
var times_names=[“小时”、“分钟”、“秒”];
var-tmp=null;
对于(变量i=0;i 0)
{
toReturnTime.push(tmp+times_names[i]+(tmp==1?'''s');
toReturnTime.push(',');
}
}
如果(toReturnTime.length/2>=2)
{
toReturnTime.pop();
tmp=toReturnTime.pop();
toReturnTime.pop();
toReturnTime.push('和'+tmp);
}
其他的
toReturnTime.pop();
返回返回返回时间。加入(“”);
}
var日期,时间限制;
函数secondCountdown()
{
date.setSeconds(date.getSeconds()-1);
timeleft.innerHTML=date.toTimeString();
如果((date.getHours()+date.getMinutes()+date.getSeconds())>0)
setTimeout(“secondCountdown()”,1000);
其他的
timeleft.innerHTML='ITS OVER';
}
函数init()
{
timeleft=document.getElementById(“timeleft”);
var time_split=timeleft.innerHTML.split(“:”);
日期=新日期(0,0,0,parseInt(时间分割[0]),parseInt(时间分割[1]),parseInt(时间分割[2]);
第二次倒计时();
}
window.onload=init;

Ooh,刚才看到我在这方面有点慢,但它似乎有效,所以我还是要添加它。:)

函数parseTime(){
var timeLeftStr;
var-timeLeft=0;
timeLeftStr=document.getElementById(“timeleft”).innerHTML;
timeLeftStr.replace(/(\d+):(\d+):(\d+)/,函数(){
对于(var i=1;i
编辑以删除
跳过

编辑2要删除正则表达式的开始和结束检查,请添加
以及
支持


编辑3以使用
getUTCHours
等,因此它可以在其他时区(OOP)中工作

你想用PHP还是JavaScript?我想用JS编写脚本,但时间最初是用PHP编写的。我把行改成了完整的,但似乎不起作用。谢谢你的帮助。我用另一个脚本交换了它,而计时器却静止不动。@SamiDzHamida这可能是因为
跳过了
。我不知道skip正在做什么,但是因为它是在原版中,我把它留下了。如果您不需要它,只需删除
If
(当然还可以调整
else
)。当我写这篇文章的时候,我把它去掉了。好的,谢谢。为了提供更多帮助,我添加了一个PHP端外观的编辑。似乎仍然不起作用你有MSN或其他我们可以讨论的东西吗?这里有一个链接,指向代码工作(至少对我来说:)。(由于某种原因,在JSFIDLE中似乎不起作用。)如果有帮助,请告诉我。很遗憾,我不在MSN上。谢谢你的帮助。这似乎对我不起作用:(
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");
}
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;
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;