网站地图    收藏   

主页 > 前端 > javascript >

同一页面可多次使用的倒计时js代码

来源:自学PHP网    时间:2014-09-19 14:48 作者: 阅读:

[导读] 在网上找了很多js倒计时代码但都只能同一页面使用一次了,最后从一朋友博客看到了一个页面可多次调用倒计时js代码下面我来分享给大家。...

今天由于需要做一个一个页面多个倒计时这样一个功能,但是网上找了很久只看见jquery有些已经写好了的插件。
但是我的项目不能用jquery(原因太多了,比如和系统库冲突),还有就是为了这么一个功能就引入80K的大小的jquery库,
也有点浪费,还得加上插件库,就更加大了,
迫于无赖,就找ecshop的一个js修改成了一个页面多个倒计时,虽然不是很完善,但是基本可以用
留给那些需要的人吧;

 

/**
修改为一个页面可以显示多个倒计时

调用方式:
onload_leftTime(开始时间戳,结束时间戳, 显示的ID);
如:

onload_leftTime(1322603631,1352793600,’leftTime’);
onload_leftTime(1322603631,1352793600,’leftTime1′);
onload_leftTime(1322603631,1352793600,’leftTime2′);

**/

 代码如下 复制代码

var timerRunning = [];
var timerID = [];
var auctionDate = [];
var startTime = [];
var showTimeId = [];

function showtime() {
var _day = ‘天’;
var _hour = ‘小时’;
var _minute = ‘分’;
var _second = ‘秒’;
var _end = ‘结束’;
var dateLeft = 0;
var hourLeft = 0;
var minuteLeft = 0;
var secondLeft = 0;
var hourZero = ”;
var minuteZero = ”;
var secondZero = ”;

var c = auctionDate.length;

for ( var i = 0, j = auctionDate.length; i < j; i++) {

if (auctionDate[i] == null) {
c–;
continue;
}
now = new Date();
var ts = parseInt((startTime[i] – now.getTime()) / 1000) + auctionDate[i];

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) {
hourZero = ’0′;
}
if (minuteLeft < 10) {
minuteZero = ’0′;
}
if (secondLeft < 10) { secondZero = ’0′; } if (dateLeft > 0) {
Temp = dateLeft + _day + hourZero + hourLeft + _hour + minuteZero + minuteLeft + _minute + secondZero + secondLeft + _second;
} else {
if (hourLeft > 0) {
Temp = hourLeft + _hour + minuteZero + minuteLeft + _minute + secondZero + secondLeft + _second;
} else {
if (minuteLeft > 0) {
Temp = minuteLeft + _minute + secondZero + secondLeft + _second;
} else {
if (secondLeft > 0) {
Temp = secondLeft + _second;
} else {
Temp = ”;
}
}
}
}

if (ts Temp = "<strong>" + _end + "</strong>";
auctionDate[i] = null;
}

if (document.getElementById(showTimeId[i])) {
document.getElementById(showTimeId[i]).innerHTML = Temp;
}

timerRunning[i] = true;

}

if (c > 0) {
timerID = setTimeout("showtime()", 1000);
}

}

function stopclock(timerRunning, timerID) {
if (timerRunning) {
clearTimeout(timerID);
}
timerRunning = false;
}

function onload_leftTime(now_time, gmt_end_time, showTime) {

var auctionDate_tmp = 0;
var _GMTEndTime = 0;

var cur_date = new Date();

_GMTEndTime = gmt_end_time;

if (_GMTEndTime > 0) {
if (now_time == undefined) {
var tmp_val = parseInt(_GMTEndTime)
- parseInt(cur_date.getTime() / 1000
+ cur_date.getTimezoneOffset() * 60);
} else {
var tmp_val = parseInt(_GMTEndTime) – now_time;
}

if (tmp_val > 0) {
auctionDate_tmp = tmp_val;
}

}

timerRunning[timerRunning.length] = false;
timerID[timerID.length] = null;
auctionDate[auctionDate.length] = auctionDate_tmp;
startTime[startTime.length] = cur_date.getTime();
showTimeId[showTimeId.length] = showTime;

showtime();

}

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论