javascript 倒计时
程序员文章站
2022-03-02 10:58:00
...
<script>
function countDown(container,delay)
{
setInterval(function(){
var oTimer = $(container);
var OLen = oTimer.length;
var p=/(\d+)天(\d{1,2})时(\d{1,2})分(\d{1,2})秒/
var format="xx天xx时xx分xx秒";
var formatArr=format.split("xx");
for(var i=0;i<OLen;i++){
//取得时间
var html=oTimer[i].innerHTML;
var timerArr=html.match(p);
if(!timerArr)
{
oTimer[i].innerHTML='结束';
continue;
}
//把时间转换成总秒数
var sunSec=parseInt(timerArr[1])*86400+parseInt(timerArr[2])*3600+parseInt(timerArr[3])*60+parseInt(timerArr[4])
sunSec = sunSec-parseInt(delay/1000);
//把时间变回时间字符串
var d=parseInt(sunSec/86400);
var h=parseInt((sunSec%86400)/3600);
var m=parseInt(((sunSec%86400)%3600)/60);
var s=parseInt(((sunSec%86400)%3600)%60);
oTimer[i].innerHTML=d+formatArr[1]+h+formatArr[2]+m+formatArr[3]+s+formatArr[4];
}
},delay)
}
countDown(".timer",1000)
</script>
转载于:https://my.oschina.net/u/781909/blog/309174