要用Js写一个倒计时功能
在Js里面用好window.setTimeOut和clearInterval即可
简单应用是可以写出来的
如何写成一个通用的timer类呢
下面是一位js达人贡献的(- ^ -)
---初版
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> JS Timer </title>
<meta name="author" content="caikanxp" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
* {font-family: 宋体;}
</style>
<script type="text/javascript">
<!--
(function() {
var Timer = function(destTime, interval, onTimeCount, onTimeUp) {
this.destTime = destTime || new Date();
this.interval = interval || 100;
this.onTimeCount = onTimeCount;
this.onTimeUp = onTimeUp;
};
Timer.prototype.run = function(interval) {
var interval = interval || this.interval;
var timer = this;
if (!this.intervalId) {
this.intervalId = window.setInterval(function() {
var now = new Date();
var time = timer.destTime - now;
var remainder = new Remainder(time);
if (typeof timer.onTimeCount == 'function') {
timer.onTimeCount(timer, remainder);
}
if (time <= 0 && !timer.timeUp) {
timer.timeUp = true;
if (typeof timer.onTimeUp == 'function') {
timer.onTimeUp(timer, remainder);
}
} else if (time > 0) {
timer.timeUp = false;
}
}, interval);
}
};
Timer.prototype.stop = function() {
if (this.intervalId) {
window.clearInterval(this.intervalId);
delete this.intervalId;
}
};
var Remainder = function(time) {
this.time = time;
this.negative = this.time < 0;
this.N = this.negative && '-' || ' ';
this.n = this.negative && '-' || '';
this.I = Math.abs(time);
this.i = this.I % 1000;
this.S = (this.I - this.i) / 1000;
this.s = this.S % 60;
this.M = (this.S - this.s) / 60;
this.m = this.M % 60;
this.H = (this.M - this.m) / 60;
this.h = this.H % 24;
this.D = (this.H - this.h) / 24;
this.d = this.D % 7
this.W = (this.D - this.d) / 7;
this.w = this.W;
}
Remainder.prototype.format = function(pattern) {
var THIS = this;
var isPattern = true;
return pattern.replace(/''|'|n|N|w+|W+|d+|D+|h+|H+|m+|M+|s+|S+|i+|I+/g, function($0) {
if ("''" == $0) {
return "'";
} else if ("'" == $0) {
isPattern = !isPattern;
return '';
} else if (/n|N/.test($0)) {
return THIS[$0];
} else if (isPattern) {
var v = new String(THIS[$0.charAt(0)]);
var p = $0.length + 1 - v.length;
p = p > 1 ? new Array(p).join(0) : '';
return p + v;
} else {
return $0;
}
});
}
window.Timer = Timer;
})();
function $(id){return document.getElementById(id);};
function changeDest() {
var dest;
try {
var m = parseInt(eval($('type').value));
var n = parseFloat($('dest').value);
n = n * m;
if (!isNaN(n)) {
dest = new Date(new Date().getTime() + n);
}
} catch (e) {
dest = new Date($('dest').value);
}
if (!isNaN(dest)) {
myTimer.destTime = dest;
}
}
window.onload = function() {
window.myTimer = new Timer(null, 10, function(timer, remainder) {
var dest = timer.destTime;
var pattern = [];
pattern.push("'离 " + dest.toString() + "'");
pattern.push("'离 " + dest.toLocaleString() + "'");
pattern.push("还有:NW 周 d 天 h 时 m 分 s 秒 i 毫秒");
pattern.push("还有:ND 天 hh 时 mm 分 ss 秒");
pattern.push("还有:nHH:mm:ss.iii");
pattern.push("还有:nI 'ms'");
$('output').innerHTML = remainder.format(pattern.join('<br />'));
});
myTimer.run();
};
//-->
</script>
</head>
<body>
<form onsubmit="changeDest(); return false;">
<input type="submit" value="更改目标时间为" />
<input type="text" id="dest" value="0.1" />
<select id="type">
<option value="1">毫秒之后</option>
<option value="1000">秒之后</option>
<option value="1000*60" selected="selected">分钟之后</option>
<option value="1000*60*60">小时之后</option>
<option value="1000*60*60*24">天之后</option>
<option value="1000*60*60*24*7">周之后</option>
<option value="date string">(指定时间 yyyy/MM/dd hh:mm:ss)</option>
</select>
</form>
<input type="button" value="运行" onclick="myTimer.run();" />
<input type="button" value="停止" onclick="myTimer.stop();" />
<label for="autostop">
<input type="checkbox" id="autostop" onclick="myTimer.onTimeUp = this.checked && function() {this.stop();} || null;" />到时后自动停止
</label>
<h1 id="output"></h1>
</body>
</html>
希望可以一起学习加以改进适合在自己的应用中
下面带有附近---
还在整理加强版本中......
分享到:
相关推荐
JS倒计时
javascript 倒计时 实例 css jquery 一个jquery倒计时程序。
闲着没事 发个倒计时代码 可以精确到毫秒的
javascript实现倒计时
js倒计时,能够获取服务器当前时间进行倒数计时
日常开发中,电商经常使用到抢购,不过当抢购的时候会用到倒计时
jquery JS 倒计时 可调节倒计时的时间,格式
js时分秒倒计时,某教育网站线上使用的同款js倒计时。
* 倒计时类使用方式 hbj ************************************************** * 多个倒计时 * * new countdown().init('end_time',1); * * 第一个参数 id * * 第二个参数 * * 不填写 内容正数则加 负数则减 ...
JS倒计时, 两个时间对比, 得到天+时+分+秒, 倒计时, ajax获取结束时间倒计时
js倒计时,javascript代码编写
一款在倒计时结束后,报警提示铃声自动响起的js倒计时代码特效。
纯js倒计时,外观简洁,实用,可以设定日期倒计时,代码量少
JS倒计时源码,倒计时有天、小时、分、秒,可根据需要进行调整
javascript 实现网页 倒计时 代码
等待下班js倒计时代码等待下班js倒计时代码等待下班js倒计时代码
js倒计时代码,挺简单的代码,新手可以参考一下
倒计时JS,非常装箱单的JS倒计时,大家可以很好的应用到网页设计中去的,哈哈
倒计时,js倒计时,可循环倒计时,倒计时结束返回函数。
php倒计时同时时间到了,自动提交表单,php+js,可以直接使用