2012-04-27 102 views
0

我想添加前导零分钟和秒在这个JavaScript倒数计时器我无法得到领先的零功能工作。额外leadingzero功能,我增加似乎并不奏效:在JavaScript倒数计时器中添加前导零分钟和秒

<pre> <html> 
<head> 
</head. 
<body> 

<p style="font-size:100px;"> 

<div id="countdown"></div> </p> 
<div id="notifier"></div> 
<script type="text/javascript"> 

    function display(notifier, str) { 
    document.getElementById(notifier).innerHTML = str; 
    } 

function toMinuteAndSecond(x) { 
    return Math.floor(x/60) + ":" + x%60; 
} 



    function setTimer(remain, actions) { 
    (function countdown() { 
    display("countdown", toMinuteAndSecond(remain));   
    actions[remain] && actions[remain](); 
    (remain -= 1) >= 0 && setTimeout(arguments.callee, 1000); 
    })(); 
} 

function leadingzero(setTimer) { 

if (setTimer < 10 && setTimer >=0) 
     return '0' + setTimer; 
     else 
      return setTimer ; } 

{ 
} 

    setTimer(600, { 
10: function() { display("notifier", "Just 10 seconds to go"); }, 
5: function() { display("notifier", "5 seconds left");  }, 
0: function() { display("notifier", "Your access is no longer guaranteed.. you need to refresh your page to gain another spot");  } 
}); 
</script> 
</body> 
</html> 

</pre> 
+0

什么是“不工作”是什么意思?什么是输出? – 2012-04-27 01:56:18

+0

该代码的作品,但问题是输出为9:8,9分钟和8秒,而不是09:08 – rome 2012-04-27 01:58:56

+3

然后,显然,'返回'0'+ setTimer;'从未得到执行。我甚至都没有看到''zero()'被调用... – 2012-04-27 02:42:59

回答

2
function toMinuteAndSecond(x) { 
    mins = Math.floor(x/60) 
    secs = x%60 
    y = ((mins>0) ? ((mins>9) ? mins : '0'+mins) + ":" : "") 
    y += (secs>9 || mins == 0) ? secs : '0'+secs; 
    return y 
} 
+0

+1 – Cody 2014-10-30 07:06:43