我给你我的代码,你参考一下,把这些放在head和/head之间
创新互联主打移动网站、成都做网站、成都网站制作、成都外贸网站建设、网站改版、网络推广、网站维护、域名注册、等互联网信息服务,为各行业提供服务。在技术实力的保障下,我们为客户承诺稳定,放心的服务,根据网站的内容与功能再决定采用什么样的设计。最后,要实现符合网站需求的内容、功能与设计,我们还会规划稳定安全的技术方案做保障。
span id="clock"00:00:05:00/span
input id="startB" type="button" value="start countdown!" onclick="run()"
input id="endB" type="button" value="stop countdown!" onclick="stop()"
br
input id="diff" type="text"
input id="next" type="text"
script language="Javascript"
var normalelapse = 100;
var nextelapse = normalelapse;
var counter;
var startTime;
var start = clock.innerText;
var finish = "00:00:00:00";
var timer = null;
// 开始运行
function run() {
startB.disabled = true;
endB.disabled = false;
counter = 0;
// 初始化开始时间
startTime = new Date().valueOf();
// nextelapse是定时时间, 初始时为100毫秒
// 注意setInterval函数: 时间逝去nextelapse(毫秒)后, onTimer才开始执行
timer = window.setInterval("onTimer()", nextelapse);
}
// 停止运行
function stop() {
startB.disabled = false;
endB.disabled = true;
window.clearTimeout(timer);
}
window.onload = function() {
endB.disabled = true;
}
// 倒计时函数
function onTimer()
{
if (start == finish)
{
window.clearInterval(timer);
alert("time is up!");
return;
}
var hms = new String(start).split(":");
var ms = new Number(hms[3]);
var s = new Number(hms[2]);
var m = new Number(hms[1]);
var h = new Number(hms[0]);
ms -= 1;
if (ms 0)
{
ms = 9;
s -= 1;
if (s 0)
{
s = 59;
m -= 1;
}
if (m 0)
{
m = 59;
h -= 1;
}
}
var ms = ms 10 ? ("0" + ms) : ms;
var ss = s 10 ? ("0" + s) : s;
var sm = m 10 ? ("0" + m) : m;
var sh = h 10 ? ("0" + h) : h;
start = sh + ":" + sm + ":" + ss + ":" + ms;
clock.innerText = start;
// 清除上一次的定时器
window.clearInterval(timer);
// 自校验系统时间得到时间差, 并由此得到下次所启动的新定时器的时间nextelapse
counter++;
var counterSecs = counter * 100;
var elapseSecs = new Date().valueOf() - startTime;
var diffSecs = counterSecs - elapseSecs;
nextelapse = normalelapse + diffSecs;
//diff.value = counterSecs + "-" + elapseSecs + "=" + diffSecs;
//next.value = "nextelapse = " + nextelapse;
if (nextelapse 0) nextelapse = 0;
// 启动新的定时器
timer = window.setInterval("onTimer()", nextelapse);
}
/script
[img]meta http-equiv="Page-Enter" content="revealTrans(duration=10, transition=4)"SCRIPT LANGUAGE="JavaScript"
var maxtime ="3";
function CountDown(){
if(maxtime=0){
minutes = Math.floor(maxtime/60);
seconds = Math.floor(maxtime%60);
msg = "你的文字说明"+minutes+"分"+seconds+"秒";//动态显示剩余时间。
document.all["timer"].innerHTML=msg;
//if(maxtime == 3) document.all["timer"].innerHTML='只剩3秒!';
--maxtime;
}
else{
clearInterval(timer);
document.all["timer"].innerHTML='123';
}
}
timer = setInterval("CountDown()",1000);
/SCRIPT
div id=timer/div
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /
title New Document /title
meta name="Generator" content="EditPlus"
meta name="Author" content="DAYU"
meta name="Keywords" content=""
meta name="Description" content=""
/head
body
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /
title New Document /title
meta name="Generator" content="EditPlus"
meta name="Author" content="DAYU"
meta name="Keywords" content=""
meta name="Description" content=""
/head
?=date_default_timezone_set("Asia/Shanghai");?!-- 这里会输出1先不管 --
?php
//3天后的时间
$d =strtotime('+3 days');
$threeday=date('Y-m-d H:i:s',$d);
?
script language="javascript"
function countdown(endtime, today)
{
//today = new Date();
//var testDate = new Date();
//today = new Date();
//target_time=new Date(endtime);
testDate = new Date();
today = testDate.format("yyyy-MM-dd hh:mm:ss");
target_time=endtime;
//alert(target_time);
//timeold=(target_time.getTime()-today.getTime());
//alert(target_time);
//alert(today);return;
//--------------------------------
timeold =DateDiff(target_time, today);
//alert(DateDiff(target_time, today));
//------------------------------
sectimeold=timeold/1000;
secondsold=Math.floor(sectimeold);//走到这里好像就有问题了
msPerDay=24*60*60*1000;
e_daysold=timeold/msPerDay;
daysold=Math.floor(e_daysold);
e_hrsold=(e_daysold-daysold)*24;
hrsold=Math.floor(e_hrsold);
e_minsold=(e_hrsold-hrsold)*60;
minsold=Math.floor(e_minsold);
e_seconds=(e_minsold-minsold)*60;
seconds=Math.floor(e_seconds);
e_millisecond=(e_seconds-seconds)*1000;
millisecond=Math.floor(e_millisecond);
millisecond10=Math.floor(millisecond);
the_element = document.getElementById('counttime');
the_element.innerHTML="仅剩br "+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";
window.setTimeout("countdown('" + endtime + "', today)", 100);
}
/script
BODY
tabletrtd width="175"SPAN id="counttime" style="FONT-WEIGHT: bold; COLOR: #000000; FONT-FAMILY:Arial"/SPAN
script language="javascript"
/**
* 时间对象的格式化;
*/
Date.prototype.format = function(format){
/*
* eg:format="yyyy-MM-dd hh:mm:ss";
*/
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
//比较2个时间的差值
function DateDiff(dmEndDate, dmStartDate) {
dmEndDate = dmEndDate.replace(/-/g, "/");
dmEndDate = new Date(dmEndDate)
dmStartDate = dmStartDate.replace(/-/g, "/");
dmStartDate = new Date(dmStartDate)
var time = dmEndDate.getTime() - dmStartDate.getTime();
var iDays = parseInt(time / (1000 * 60 * 60 * 24));
return iDays
}
//*****************************************
// var endtime = "?=date('Y-m-d H:i:s',time());?";
//var testDate = new Date();
//var today = testDate.format("yyyy年MM月dd日hh小时mm分ss秒");
//var today = testDate.format("yyyy-MM-dd hh:mm:ss");
//var endtime = '6-2-2013 17:35:00';
//var endtime = "?=date('Y-m-d H:i:s',time());?";
var endtime = "?=$threeday;?";
//alert(endtime);
//var today = new Date();
var testDate = new Date();
var today = testDate.format("yyyy-MM-dd hh:mm:ss");
// alert(today)
//countdown("?=date('Y-m-d H:i:s',time());?", today);
countdown("?=$threeday;?", today);
/script/td/tr/table
/BODY
/HTML
/body
/html
/body
/html
-------------------------------------------------------
页面保存为PHP文件去执行.
仅剩
0天0小时0分0秒//这个结果不对,应该是2天几分几秒才对
我给你提供思路。
退格你可能需要用到字符串函数,substring(1)来截取不包含第一个字符的后面所有的字符,然后把值传给文本框。
求倒数,你需要定义一个临时变量,把分母和分子的的值作交换就行了。
代码你自己写,不难的。
我用纯JS实现不了刷新页面还能保持刷新前的数据
这个需要把开始倒数时的时间传入后台,利用后台做出时间的调整,就是获取倒数5分钟之后是什么时间,刷新得到调整后的时间再和当前时间做出对比得到需要倒数的时间,然后继续倒数。
如果有高手可以实现刷新页面可以得到上一次页面的JS信息,能继续倒数的话请发上来共同学习。