0

我正在尝试在这个 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>
4

1 回答 1

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
}
于 2013-02-01T18:18:46.853 回答