JS倒计时问题
<scriptlanguage="javascript">varwait=<%=lingqutime%>;//设置秒数(单位秒)varsecs=0;for(vari=1;...
<script language="javascript">
var wait = <%=lingqutime%>; //设置秒数(单位秒)
var secs = 0;
for(var i=1;i<=wait;i++)
{
window.setTimeout("sTimer("+i+")",i*1000);
}
function sTimer(num)
{
if(num==wait)
{
document.getElementById("BtnOk").value=" 点击领取 ";
document.getElementById("BtnOk").disabled=false;
}
else
{
secs=wait-num;
document.getElementById("BtnOk").value="("+secs+")秒后开放领取";
}
}
//-->
</script>
当我这个 lingqutime的值大于10000之后就感觉打开页面很卡,请问有别的方法实现这个效果吗。
我要达到的目的就是 控制一个按钮,在指定时间之后才可以点。不然就在按钮上显示几秒后可以按。这个指定时间有可能是1万也有可能是10万。 展开
var wait = <%=lingqutime%>; //设置秒数(单位秒)
var secs = 0;
for(var i=1;i<=wait;i++)
{
window.setTimeout("sTimer("+i+")",i*1000);
}
function sTimer(num)
{
if(num==wait)
{
document.getElementById("BtnOk").value=" 点击领取 ";
document.getElementById("BtnOk").disabled=false;
}
else
{
secs=wait-num;
document.getElementById("BtnOk").value="("+secs+")秒后开放领取";
}
}
//-->
</script>
当我这个 lingqutime的值大于10000之后就感觉打开页面很卡,请问有别的方法实现这个效果吗。
我要达到的目的就是 控制一个按钮,在指定时间之后才可以点。不然就在按钮上显示几秒后可以按。这个指定时间有可能是1万也有可能是10万。 展开
2个回答
展开全部
<!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>
<table><tr><td 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天几分几秒才对
<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>
<table><tr><td 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天几分几秒才对
展开全部
<script language="javascript">
var wait = <%=lingqutime%>; //设置秒数(单位秒)
var i = 0;
var iid = 0;
iid = setInterval("sTimer()",1000);
function sTimer()
{
var r=wait-i;
if(r==0)
{
clearInterval(iid);
document.getElementById("BtnOk").value=" 点击领取 ";
document.getElementById("BtnOk").disabled=false;
}
else
{
document.getElementById("BtnOk").value="("+r+")秒后开放领取";
i++;
}
}
//-->
var wait = <%=lingqutime%>; //设置秒数(单位秒)
var i = 0;
var iid = 0;
iid = setInterval("sTimer()",1000);
function sTimer()
{
var r=wait-i;
if(r==0)
{
clearInterval(iid);
document.getElementById("BtnOk").value=" 点击领取 ";
document.getElementById("BtnOk").disabled=false;
}
else
{
document.getElementById("BtnOk").value="("+r+")秒后开放领取";
i++;
}
}
//-->
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询