在Javascript倒计时器中将前导零添加到分钟和秒

时间:2012-04-27 01:50:56

标签: javascript

我正在尝试在此javascript倒计时器中添加前导零到分钟和秒钟,我无法使领先的零功能工作。我添加的额外的Leadingzero功能似乎不起作用:

 <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>

1 个答案:

答案 0 :(得分: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
}
相关问题