求一个ajax的倒计时程序~
1个回答
展开全部
刚刚写的代码
/*cxty@msn.com*/
var t_bTime = new Array();//存储开始时间
var t_eTime = new Array();//存储结束时间
var t_tObj = new Array();//存储显示对象
var loop_TimeID = null;
function clearLoop()
{
clearTimeout(loop_TimeID);
t_bTime = new Array();
t_eTime = new Array();
t_tObj = new Array();
}
function Add_t_Time(bTime,eTime,RObj)
{
t_bTime[t_bTime.length] = bTime;
t_eTime[t_eTime.length] = eTime;
t_tObj[t_tObj.length] = RObj;
CheckTime();
}
function CheckTime()
{//倒计时
var i=0;
if(t_eTime.length>0)
{
for(i=0;i<t_eTime.length;i++)
{
if(Sys.getObj(t_tObj[i]))
{
//alert(t_bTime[i]+'\n'+t_eTime[i]);
var bTime = Date_Ex(t_bTime[i]);
var eTime = Date_Ex(t_eTime[i]);
var now = new Date().getTime();
//alert(now);
bTime = bTime.valueOf();
eTime = eTime.valueOf();
now = now.valueOf();
var nTime;
var ts;
var wStr='';
var dateLeft = 0;
var hourLeft = 0;
var minuteLeft = 0;
var secondLeft = 0;
var CurHour = 0;
var CurMinute = 0;
var CurSecond = 0;
var Temp;
var isEnd = false;
if(bTime>now)
{
//nTime = new Date(bTime-now);
ts=parseInt((bTime-now)/1000);
wStr = '开始';
}
else
{
//nTime = new Date(eTime-now);
if(eTime>now)
{
ts=parseInt((eTime-now)/1000);
wStr = '结束';
}
else
{
ts = 0;
isEnd = true;
}
}
if(ts < 0)
{
ts = 0;
CurHour = 0;
CurMinute = 0;
CurSecond = 0;
} else {
dateLeft =parseInt(ts/86400);
ts = ts - dateLeft * 86400;
hourLeft = parseInt(ts/3600);
ts = ts - hourLeft * 3600;
minuteLeft = parseInt(ts/60);
secondLeft = ts - minuteLeft * 60;
}
if(hourLeft < 10) hourLeft = '0' +hourLeft;
if(minuteLeft < 10) minuteLeft = '0' +minuteLeft;
if(secondLeft<10) secondLeft='0'+secondLeft;
if( dateLeft > 0 )
dateLeft = dateLeft + '天' ;
else
dateLeft = "";
if( hourLeft > 0 )
hourLeft = hourLeft + '小时' ;
else
{
if( dateLeft != "" )
hourLeft = "00" + '小时';
else
hourLeft = "";
}
if( minuteLeft > 0 )
minuteLeft = minuteLeft + '分钟' ;
else
{
if( dateLeft !="" || hourLeft != "")
minuteLeft = "00" + '分钟';
else
minuteLeft = "";
}
if( secondLeft > 0 )
secondLeft = secondLeft + '秒' ;
else
{
if( dateLeft !="" || hourLeft != "" || minuteLeft != "")
secondLeft = "00" + '秒';
else
secondLeft = "";
}
if (dateLeft == '') {
Temp=dateLeft+hourLeft+minuteLeft+secondLeft ;
}else {
Temp=dateLeft+hourLeft;
}
if(isEnd)
{
Sys.getObj(t_tObj[i]).innerHTML = '已经结束';
}
else
{
Sys.getObj(t_tObj[i]).innerHTML = '还有 '+ Temp +' '+wStr;
}
nTime = null;
}
}
}
i = null;
loop_TimeID = setTimeout("CheckTime()",1000);
}
用来获取对象的
Sys = function(){;}
Sys.getObj = function(objId){if (document.getElementById)return document.getElementById(objId);else if
(document.all)return document.all(objId);};
调用方法
先
clearLoop();
然后
Add_t_Time(开始时间,结束时间,要在哪个对象上显示)
时间格式
yyyy-MM-dd hh:mm:ss
显示效果
还未到达开始时间:还有 x天x小时x秒 开始
还未到达结束时间:还有 x天x小时x秒 结束
已经结束: 已经结束
需要可以拿取,根据自己需求改改就行了
/*cxty@msn.com*/
var t_bTime = new Array();//存储开始时间
var t_eTime = new Array();//存储结束时间
var t_tObj = new Array();//存储显示对象
var loop_TimeID = null;
function clearLoop()
{
clearTimeout(loop_TimeID);
t_bTime = new Array();
t_eTime = new Array();
t_tObj = new Array();
}
function Add_t_Time(bTime,eTime,RObj)
{
t_bTime[t_bTime.length] = bTime;
t_eTime[t_eTime.length] = eTime;
t_tObj[t_tObj.length] = RObj;
CheckTime();
}
function CheckTime()
{//倒计时
var i=0;
if(t_eTime.length>0)
{
for(i=0;i<t_eTime.length;i++)
{
if(Sys.getObj(t_tObj[i]))
{
//alert(t_bTime[i]+'\n'+t_eTime[i]);
var bTime = Date_Ex(t_bTime[i]);
var eTime = Date_Ex(t_eTime[i]);
var now = new Date().getTime();
//alert(now);
bTime = bTime.valueOf();
eTime = eTime.valueOf();
now = now.valueOf();
var nTime;
var ts;
var wStr='';
var dateLeft = 0;
var hourLeft = 0;
var minuteLeft = 0;
var secondLeft = 0;
var CurHour = 0;
var CurMinute = 0;
var CurSecond = 0;
var Temp;
var isEnd = false;
if(bTime>now)
{
//nTime = new Date(bTime-now);
ts=parseInt((bTime-now)/1000);
wStr = '开始';
}
else
{
//nTime = new Date(eTime-now);
if(eTime>now)
{
ts=parseInt((eTime-now)/1000);
wStr = '结束';
}
else
{
ts = 0;
isEnd = true;
}
}
if(ts < 0)
{
ts = 0;
CurHour = 0;
CurMinute = 0;
CurSecond = 0;
} else {
dateLeft =parseInt(ts/86400);
ts = ts - dateLeft * 86400;
hourLeft = parseInt(ts/3600);
ts = ts - hourLeft * 3600;
minuteLeft = parseInt(ts/60);
secondLeft = ts - minuteLeft * 60;
}
if(hourLeft < 10) hourLeft = '0' +hourLeft;
if(minuteLeft < 10) minuteLeft = '0' +minuteLeft;
if(secondLeft<10) secondLeft='0'+secondLeft;
if( dateLeft > 0 )
dateLeft = dateLeft + '天' ;
else
dateLeft = "";
if( hourLeft > 0 )
hourLeft = hourLeft + '小时' ;
else
{
if( dateLeft != "" )
hourLeft = "00" + '小时';
else
hourLeft = "";
}
if( minuteLeft > 0 )
minuteLeft = minuteLeft + '分钟' ;
else
{
if( dateLeft !="" || hourLeft != "")
minuteLeft = "00" + '分钟';
else
minuteLeft = "";
}
if( secondLeft > 0 )
secondLeft = secondLeft + '秒' ;
else
{
if( dateLeft !="" || hourLeft != "" || minuteLeft != "")
secondLeft = "00" + '秒';
else
secondLeft = "";
}
if (dateLeft == '') {
Temp=dateLeft+hourLeft+minuteLeft+secondLeft ;
}else {
Temp=dateLeft+hourLeft;
}
if(isEnd)
{
Sys.getObj(t_tObj[i]).innerHTML = '已经结束';
}
else
{
Sys.getObj(t_tObj[i]).innerHTML = '还有 '+ Temp +' '+wStr;
}
nTime = null;
}
}
}
i = null;
loop_TimeID = setTimeout("CheckTime()",1000);
}
用来获取对象的
Sys = function(){;}
Sys.getObj = function(objId){if (document.getElementById)return document.getElementById(objId);else if
(document.all)return document.all(objId);};
调用方法
先
clearLoop();
然后
Add_t_Time(开始时间,结束时间,要在哪个对象上显示)
时间格式
yyyy-MM-dd hh:mm:ss
显示效果
还未到达开始时间:还有 x天x小时x秒 开始
还未到达结束时间:还有 x天x小时x秒 结束
已经结束: 已经结束
需要可以拿取,根据自己需求改改就行了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询