Javascript计时器1分钟

时间:2017-05-18 05:29:08

标签: javascript

这是一个小代码,其中按钮单击倒计时开始如何在用户打开该页面时直接开始倒计时。

a=55
export a
perl -e 'use POSIX qw(strftime); print strftime "%a %b %e %H:%M:%S %Y",localtime(time()+ 3600*24*$ENV{a});'

5 个答案:

答案 0 :(得分:0)

尝试使用window.onload firefox使用<body onload="onTimer()">

window.onload= function(){
onTimer()
}
i = 60;

function onTimer() {
  document.getElementById('mycounter').innerHTML = i;
  i--;
  if (i < 0) {
    alert('You lose!');
  } else {
    setTimeout(onTimer, 1000);
  }
}
<button onclick="onTimer()"> Clickme
  </button>
    <div id="mycounter">
      </div>

答案 1 :(得分:0)

只需在body标签中调用onLoad事件:

<body onload="onTimer()">

答案 2 :(得分:0)

http://jsfiddle.net/BH8q9/

检查演示

HTML

<span>0:60</span>

JQuery的

var interval = setInterval(function() {
var timer = $('span').html();
timer = timer.split(':');
var minutes = parseInt(timer[0], 10);
var seconds = parseInt(timer[1], 10);
seconds -= 1;
if (minutes < 0) return clearInterval(interval);
if (minutes < 10 && minutes.length != 2) minutes = '0' + minutes;
if (seconds < 0 && minutes != 0) {
    minutes -= 1;
    seconds = 59;
}
else if (seconds < 10 && length.seconds != 2) seconds = '0' + seconds;
$('span').html(minutes + ':' + seconds);

if (minutes == 0 && seconds == 0)
    clearInterval(interval);
}, 1000);

答案 3 :(得分:0)

onTimerPage加载时执行document

Working Demo

&#13;
&#13;
i = 60;
function onTimer() {
document.getElementById('mycounter').innerHTML = i;
i--;
if (i < 0) {
alert('You lose!');
}
else {
setTimeout(onTimer, 1000);
}
}

onTimer();
&#13;
<div id="mycounter"></div>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

i = 60;
function onTimer() {
document.getElementById('mycounter').innerHTML = i;
i--;
if (i < 0) {
alert('You lose!');
}
else {
setTimeout(onTimer, 1000);
}
}

onTimer();
<div id="mycounter">01:00</div>