倒计时
作者:我佛山人 类型:闪吧BBS 来源:闪吧个人空间
<style>
div{}
button{}
</style>
<div id="dd"> </div>
<script type="text/javascript">
<!--
String.prototype.format = function(){
var tmpStr = this;
var iLen = arguments.length;
for(var i=0;i<iLen;i++){
tmpStr = tmpStr.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
}
return tmpStr;
}
Number.prototype.We = function(bt){
var byte = bt || 2;
var data = this.toString();
return (new Array(byte + 1).join("0") + data).substring(data.length);
}
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
function CountDown(){
var ref = this;
var temp = "";
this.Timer = null;
this.Max = 0;
this.Current = 0;
this.Element = null;
this.Format = "mm:ss";
this.Step = 1000;
function formatDate(template, input){
var day = input / 60 /60 /24 | 0;
var hour = (input - day * 60* 60 * 24) / 60 /60 | 0;
var minute = (input - day * 60* 60 * 24 - hour * 60 * 60) / 60 | 0;
var second = input - day * 60* 60 * 24 - hour * 60 * 60 - minute * 60;
if(temp == ""){
temp = template.replace(/dd/g, "{0}").replace(/d/g, "{1}");
temp = temp.replace(/hh/g, "{2}").replace(/h/g, "{3}");
temp = temp.replace(/mm/g, "{4}").replace(/m/g, "{5}");
temp = temp.replace(/ss/g, "{6}").replace(/s/g, "{7}");
}
return temp.format(day.We(), day, hour.We(), hour, minute.We(), minute, second.We(), second)
}
this.Reset = function(){
this.Clear();
this.Current = this.Max;
}
this.Create = function(element){
if(typeof(element) == "string"){
if($(element) == null)document.write("<div id=\"{0}\"></div>".format(element));
this.Element = $(element);
} else this.Element = element;
}
this.Start = function(){
this.OnBeforeStart();
this.Clear();
if(this.Current == 0)this.Current = this.Max;
this.Timer = setInterval(this.ShowText, this.Step);
}
this.Stop = function(){
this.Reset();
this.OnStop();
}
this.Pause = function(){
this.Clear();
this.OnPause();
}
this.Clear = function(){
if(this.Timer != null)clearInterval(this.Timer);
}
this.ShowText = function(){
with(ref){
if(--Current > -1) Element.innerText = formatDate(Format, Current);
else {Clear();OnTimeOut();};
}
}
this.OnBeforeStart = function(){}
this.OnStop = function(){}
this.OnPause = function(){}
this.OnTimeOut = function(){}
this.toString = function(){
return "DHTML CountDown";
}
}
var cd = new CountDown();
cd.Create("ddd");
cd.Max = 60;
cd.Start();
cd.OnTimeOut = function(){alert("时间到!");}
var cd1 = new CountDown();
cd1.Format = "d天 h小时:mm:ss";
cd1.Create("dd");
cd1.Max = 300000;
cd1.Start();
//-->
</script>
<button onclick="cd.Pause()">Time2 Pause</button>
<button onclick="cd.Start()">Time2 Start</button>
<div contentEditable style="ime-mode:Disabled">123</div>
<input style="ime-mode:Disabled" value="123">
div{}
button{}
</style>
<div id="dd"> </div>
<script type="text/javascript">
<!--
String.prototype.format = function(){
var tmpStr = this;
var iLen = arguments.length;
for(var i=0;i<iLen;i++){
tmpStr = tmpStr.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
}
return tmpStr;
}
Number.prototype.We = function(bt){
var byte = bt || 2;
var data = this.toString();
return (new Array(byte + 1).join("0") + data).substring(data.length);
}
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
function CountDown(){
var ref = this;
var temp = "";
this.Timer = null;
this.Max = 0;
this.Current = 0;
this.Element = null;
this.Format = "mm:ss";
this.Step = 1000;
function formatDate(template, input){
var day = input / 60 /60 /24 | 0;
var hour = (input - day * 60* 60 * 24) / 60 /60 | 0;
var minute = (input - day * 60* 60 * 24 - hour * 60 * 60) / 60 | 0;
var second = input - day * 60* 60 * 24 - hour * 60 * 60 - minute * 60;
if(temp == ""){
temp = template.replace(/dd/g, "{0}").replace(/d/g, "{1}");
temp = temp.replace(/hh/g, "{2}").replace(/h/g, "{3}");
temp = temp.replace(/mm/g, "{4}").replace(/m/g, "{5}");
temp = temp.replace(/ss/g, "{6}").replace(/s/g, "{7}");
}
return temp.format(day.We(), day, hour.We(), hour, minute.We(), minute, second.We(), second)
}
this.Reset = function(){
this.Clear();
this.Current = this.Max;
}
this.Create = function(element){
if(typeof(element) == "string"){
if($(element) == null)document.write("<div id=\"{0}\"></div>".format(element));
this.Element = $(element);
} else this.Element = element;
}
this.Start = function(){
this.OnBeforeStart();
this.Clear();
if(this.Current == 0)this.Current = this.Max;
this.Timer = setInterval(this.ShowText, this.Step);
}
this.Stop = function(){
this.Reset();
this.OnStop();
}
this.Pause = function(){
this.Clear();
this.OnPause();
}
this.Clear = function(){
if(this.Timer != null)clearInterval(this.Timer);
}
this.ShowText = function(){
with(ref){
if(--Current > -1) Element.innerText = formatDate(Format, Current);
else {Clear();OnTimeOut();};
}
}
this.OnBeforeStart = function(){}
this.OnStop = function(){}
this.OnPause = function(){}
this.OnTimeOut = function(){}
this.toString = function(){
return "DHTML CountDown";
}
}
var cd = new CountDown();
cd.Create("ddd");
cd.Max = 60;
cd.Start();
cd.OnTimeOut = function(){alert("时间到!");}
var cd1 = new CountDown();
cd1.Format = "d天 h小时:mm:ss";
cd1.Create("dd");
cd1.Max = 300000;
cd1.Start();
//-->
</script>
<button onclick="cd.Pause()">Time2 Pause</button>
<button onclick="cd.Start()">Time2 Start</button>
<div contentEditable style="ime-mode:Disabled">123</div>
<input style="ime-mode:Disabled" value="123">
责任编辑:silvia 时间:2006年11月20日
- 最近更新
