本文目录一览:
js倒计时代码的问题
var now = new Date()
var over = new Date(2017,4,18,20,30,00)
var time=(over.getTime())-(now.getTime())
time=time/1000;//毫秒数转秒数
setInterval (function(){
var day=parseInt(time/60/60/24)
var hour=parseInt(time/60/60%24)
var minute=parseInt(time/60%60)
var second=parseInt(time%60)
time-=1;
console.log(day+"天"+hour+"时"+minute+"分"+second+"秒")
},1000)
求一个倒计时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毫秒以后执行一次本函数
};
望采纳!
时间倒计时代码
html
head
title倒计时/title
body
script language=JavaScript
var now = new Date("11/26/2006 " + 10 + ":" + 30 + ":" + 30);
function GetServerTime()
{
var urodz = new Date("11/30/2006 11:38:33");
now.setTime(now.getTime()+250);
days = (urodz - now) / 1000 / 60 / 60 / 24;
daysRound = Math.floor(days);
hours = (urodz - now) / 1000 / 60 / 60 - (24 * daysRound);
hoursRound = Math.floor(hours);
minutes = (urodz - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
minutesRound = Math.floor(minutes);
seconds = (urodz - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
secondsRound = Math.round(seconds);
document.getElementById("date").innerHTML = daysRound;
document.getElementById("time").innerHTML = hoursRound + ":" + minutesRound + ":" + secondsRound;
}
setInterval("GetServerTime()",250);
/script
距离********还有span id="date"/span span class="white14b"天/span span id="time"/span
/body
/html
用js写倒计时~但规定要用以下代码~求助
SCRIPT LANGUAGE="JavaScript"
!--
var maxtime = 60*60;//60*60 //一个小时,按秒计算,自己调整!
function CountDown(){
if(maxtime=0){
//minutes = Math.floor(maxtime/60);
seconds = Math.floor(maxtime%60);
//msg = "距离结束还有"+minutes+"分"+seconds+"秒";
msg = "距离结束还有"+seconds+"秒";
document.all["timer"].innerHTML=msg;
if(maxtime == 5*60){
alert('注意,还有5分钟!');
}
--maxtime;
}else{
clearInterval(timer);
//alert("时间到,结束!");
maxtime=60*60;
timer = setInterval("CountDown()",1000);
}
}
timer = setInterval("CountDown()",1000);
//--
/SCRIPT
div id="timer" style="color:red"/div
SCRIPT LANGUAGE="JavaScript"
!--
var maxtime = 5*60;//60*60 //一个小时,按秒计算,自己调整!
var timer;
function CountDown(){
if(maxtime=0){
var minutes = Math.floor(maxtime/60);
var seconds = Math.floor(maxtime%60);
var msg = "距离刷新还有"+minutes+"分"+seconds+"秒";
$('#timer').html(msg);
//document.all["timer"].innerHTML=msg;
--maxtime;
}else{
clearInterval(timer);
//一下两个方法是倒计时结束后调用的方法
searchTaskList();
myajax();
//设置下一次的倒计时
maxtime=5*60;
timer = setInterval("CountDown()",1000);
}
}
timer = setInterval("CountDown()",1000);
//--
/SCRIPT
div id="timer" style="display:inline;color:red"/div