本文目录一览:
JS 倒计时实现代码(时、分,秒)
JS实现倒计时(时、分,秒)
var
interval
=
1000;
function
ShowCountDown(year,month,day,divname)
{
var
now
=
new
Date();
var
endDate
=
new
Date(year,
month-1,
day);
var
leftTime=endDate.getTime()-now.getTime();
var
leftsecond
=
parseInt(leftTime/1000);
//var
day1=parseInt(leftsecond/(24*60*60*6));
var
day1=Math.floor(leftsecond/(60*60*24));
var
hour=Math.floor((leftsecond-day1*24*60*60)/3600);
var
minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60);
var
second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60);
var
cc
=
document.getElementById(divname);
cc.innerHTML
=
"脚本之家提示距离"+year+"年"+month+"月"+day+"日还有:"+day1+"天"+hour+"小时"+minute+"分"+second+"秒";
}
window.setInterval(function(){ShowCountDown(2010,4,20,'divdown1');},
interval);
[Ctrl+A
全选
注:如需引入外部Js需刷新才能执行]
求一串倒计时js代码
!DOCTYPE HTML
html lang="en"
head
meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" /
meta http-equiv="Content-Type" content="text/html;charset=UTF-8"
title倒计时/title
link rel="stylesheet" href="css.css" type="text/css" media="screen"
script src=""/script
/head
body
style.date_details_edd input{text-align:center;}/style
div class="second"
input type="text" value="60000" id="second"!--可以隐藏--
/div
div class="end_time"
距离活动结束还有:span0/spanstrong天/strongspan0/spanstrong小时/strongspan0/spanstrong分/strongspan0/spanstrong秒/strong
/div
script
function play(){
var s=$('#second').val(); //获得秒数
var ss=s;
var day=Math.floor(s/86400); //获取天数
s=s%86400;
var hours=Math.floor(s/3600); //小时数
s=s%3600;
var minutes=Math.floor(s/60); //分钟
s=s%60;
var s4=s;
if(day=0 hours=0 minutes=0 s4=0){
alert(1);
clearInterval(timer);
}else{
$(".end_time span").eq(0).html(fullZero(day,2)); //赋值 天数
$(".end_time span").eq(1).html(fullZero(hours,2)); //小时
$(".end_time span").eq(2).html(fullZero(minutes,2));//分钟
$(".end_time span").eq(3).html(fullZero(s4,2)); //秒
$('#second').val(ss-1);
}
}
$(function(){
var timer=setInterval(play,1000);
})
//给时间前面补0
function fullZero(_time,n){
var str=""+_time;
while(str.lengthn){
str="0"+str;
}
return str;
};
/script
/body
/html
求一个倒计时js代码,非常简单的
逻辑如下:
1、先判断两个时间的时间差,当然如果得到的时间是13位时间戳的话你需要转化一下
2、判断时间够一天显示天数,够一小时显示小时数
相关代码:
//获取时间,时间戳转换,不是则不转
var mydate1 = new Date(parseInt(time1.replace("/Date(", "").replace(")/", "")));
var mydate2 = new Date(parseInt(time2.replace("/Date(", "").replace(")/", "")));
//获取剩余秒数
var timerc =dateDiff(mydate, '2015/05/19 10:00:00') / 1000;
function dateDiff(date1, date2) {
var dt1 = new Date(Date.parse(date1));
var dt2 = new Date(Date.parse(date2));
try {
return Math.round((dt2.getTime() - dt1.getTime()));
}
catch (e) {
return e.message;
}
}
//循环调用倒计时
function add() { //加时函数
--timerc; //时间变量自减1
var day = parseInt(timerc / 86400);
var hour = parseInt((timerc % 86400) / 3600);
var min = parseInt((timerc % 3600) / 60);
var sec = Number(parseInt(timerc % 60 / 10)).toString() + Number(parseInt((timerc % 10))).toString();
if (day 0) { //如果不到5分钟
$("#day").html(day + '天'); //写入天数
$("#hour").html(hour + '时'); //写入小时数
$("#min").html(min + '分'); //写入分钟数
$("#sec").html(sec + '秒'); //写入秒数(两位)
}
else if (hour 0) {
$("#hour").html(hour + '时'); //写入小时数
$("#min").html(min + '分'); //写入分钟数
$("#sec").html(sec + '秒'); //写入秒数(两位)
}
else if (min 0) {
$("#min").html(min + '分'); //写入分钟数
$("#sec").html(sec + '秒'); //写入秒数(两位)
}
else if (sec != '00') {
$("#sec").html(sec + '秒'); //写入秒数(两位)
}
else {
tiao(activity); return true;//时间到0调用函数
}
setTimeout("add()", 1000); //设置1000毫秒以后执行一次本函数
};
望采纳!
帮忙改一个js倒计时代码
(function(timeleft) {
var minutes = 0,
seconds = 0,
caller = arguments.callee,
timer;
if(--timeleft = 0) {
minutes = Math.floor(timeleft / 60);
seconds = Math.floor(timeleft % 60);
document.all["timer"].innerHTML = "距离结束还有" + minutes + "分" + seconds + "秒";
if (timeleft == 5 * 60) {
alert('注意,还有5分钟!');
}
setTimeout(function() {
caller(timeleft);
}, 1000);
} else {
alert("时间到,结束!");
}
}(3600));