Javascript倒计时器 - 采用系统时间自校验

类别:网站制作 点击:0 评论:0 推荐:

00:01:11:00
/* This notice must be untouched at all times. countdown.js v. 1.0 The latest version is available at http://blog.csdn.net/yjgx007 Copyright (c) 2004 Xinyi.Chen. All rights reserved. Created 7/30/2004 by Xinyi.Chen. Web: http://blog.csdn.net/yjgx007 E-Mail: [email protected] Last modified: 7/30/2004 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; See the GNU General Public License at http://www.gnu.org/copyleft/gpl.html for more details. */ var normalelapse = 100; var nextelapse = normalelapse; var counter; var startTime; var start = clock.innerText; var finish = "00:00:00:00"; var timer = null; function run() { startB.disabled = true; endB.disabled = false; counter = 0; startTime = new Date().valueOf(); timer = window.setInterval("onTimer()", nextelapse); } function stop() { startB.disabled = false; endB.disabled = true; window.clearTimeout(timer); } window.onload = function() { endB.disabled = true; } function onTimer() { if (start == finish) { window.clearInterval(timer); alert("time is up!"); return; } var hms = new String(start).split(":"); var ms = new Number(hms[3]); var s = new Number(hms[2]); var m = new Number(hms[1]); var h = new Number(hms[0]); ms -= 10; if (ms < 0) { ms = 90; s -= 1; if (s < 0) { s = 59; m -= 1; } if (m < 0) { m = 59; h -= 1; } } var ms = ms < 10 ? ("0" + ms) : ms; var ss = s < 10 ? ("0" + s) : s; var sm = m < 10 ? ("0" + m) : m; var sh = h < 10 ? ("0" + h) : h; start = sh + ":" + sm + ":" + ss + ":" + ms; clock.innerText = start; window.clearInterval(timer); counter++; var counterSecs = counter * 100; var elapseSecs = new Date().valueOf() - startTime; var diffSecs = counterSecs - elapseSecs; nextelapse = normalelapse + diffSecs; diff.value = counterSecs + "-" + elapseSecs + "=" + diffSecs; next.value = "nextelapse = " + nextelapse; if (nextelapse < 0) nextelapse = 0; timer = window.setInterval("onTimer()", nextelapse); }

上个版本的倒计时器存在一些问题, 定时(0.1秒)调用函数的过程中未考虑倒计时函数中程序代
码的执行时间以及显示时间所产生的延迟, 这次利用系统时间自校验倒计时, 无需手工调校使得倒计时更为精确, 代码及详细注释如下:

<span id="clock">00:01:11:00</span>
<input id="startB" type="button" value="start countdown!" onclick="run()">
<input id="endB" type="button" value="stop countdown!" onclick="stop()">
<br>
<input id="diff" type="text">
<input id="next" type="text">
<script language="Javascript">
/* This notice must be untouched at all times.

countdown.js    v. 1.0
The latest version is available at
http://blog.csdn.net/yjgx007

Copyright (c) 2004 Xinyi.Chen. All rights reserved.
Created 7/30/2004 by Xinyi.Chen.
Web: http://blog.csdn.net/yjgx007
E-Mail: [email protected]
Last modified: 7/30/2004

This program is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation;

See the GNU General Public License
at http://www.gnu.org/copyleft/gpl.html for more details.
*/
var normalelapse = 100;
var nextelapse = normalelapse;
var counter;
var startTime;
var start = clock.innerText;
var finish = "00:00:00:00";
var timer = null;

// 开始运行
function run() {
  startB.disabled = true;
  endB.disabled = false;
  counter = 0;
  // 初始化开始时间
  startTime = new Date().valueOf();

  // nextelapse是定时时间, 初始时为100毫秒
  // 注意setInterval函数: 时间逝去nextelapse(毫秒)后, onTimer才开始执行
  timer = window.setInterval("onTimer()", nextelapse);
}

// 停止运行
function stop() {
  startB.disabled = false;
  endB.disabled = true;
  window.clearTimeout(timer);
}

window.onload = function() {
  endB.disabled = true;
}

// 倒计时函数
function onTimer()
{

if (start == finish)
{
  window.clearInterval(timer);
  alert("time is up!");
  return;
}

var hms = new String(start).split(":");
var ms = new Number(hms[3]);
var s = new Number(hms[2]);
var m = new Number(hms[1]);
var h = new Number(hms[0]);
 
ms -= 10;
if (ms < 0)
{
  ms = 90;
  s -= 1;
  if (s < 0)
  {
    s = 59;
    m -= 1;
  }
 
  if (m < 0)
  {
    m = 59;
    h -= 1;
  }
}

var ms = ms < 10 ? ("0" + ms) : ms;
var ss = s < 10 ? ("0" + s) : s;
var sm = m < 10 ? ("0" + m) : m;
var sh = h < 10 ? ("0" + h) : h;

start = sh + ":" + sm + ":" + ss + ":" + ms;
clock.innerText = start;

// 清除上一次的定时器
window.clearInterval(timer);

// 自校验系统时间得到时间差, 并由此得到下次所启动的新定时器的时间nextelapse
counter++;
var counterSecs = counter * 100;
var elapseSecs = new Date().valueOf() - startTime;
var diffSecs = counterSecs - elapseSecs;
nextelapse = normalelapse + diffSecs;
diff.value = counterSecs + "-" + elapseSecs + "=" + diffSecs;
next.value = "nextelapse = " + nextelapse;
if (nextelapse < 0) nextelapse = 0;

// 启动新的定时器
timer = window.setInterval("onTimer()", nextelapse);
}
</script>

本文地址:http://com.8s8s.com/it/it31282.htm