js设置DIV显示时间并动态显示时间倒计时多少秒,倒计时完了之后该层自动隐藏。
3个回答
展开全部
给你个思路:
1初始化时间,例如1小时5分钟30秒(也可以让用户手动设置,这里略)
保存在全局变量中
var hour,minute,second;
2设置定时每隔1秒执行function xxx
setInterval(function xxx(){...},1000);
3编写function用于每隔1秒更新时间,里面判断若倒计时为0时,隐藏div
function xxx(){
if(--second==0){
if(--minute==0){
if(--hour==0){
//隐藏div 设置style.display='none'
}
show(hour,minute,second);
second=60;
minute=60;
}
show(hour,minute,second);
second=60;
}
show(hour,minute,second);
}
function show(hour,minute,second){
var str_hour = hour<10?"0"+hour:""+hour;
var str_minute = minute<10?"0"+minute:""+minute;
var str_second = second<10?"0"+second:""+second;
//将这三个时分秒显示到div中指定位置
}
1初始化时间,例如1小时5分钟30秒(也可以让用户手动设置,这里略)
保存在全局变量中
var hour,minute,second;
2设置定时每隔1秒执行function xxx
setInterval(function xxx(){...},1000);
3编写function用于每隔1秒更新时间,里面判断若倒计时为0时,隐藏div
function xxx(){
if(--second==0){
if(--minute==0){
if(--hour==0){
//隐藏div 设置style.display='none'
}
show(hour,minute,second);
second=60;
minute=60;
}
show(hour,minute,second);
second=60;
}
show(hour,minute,second);
}
function show(hour,minute,second){
var str_hour = hour<10?"0"+hour:""+hour;
var str_minute = minute<10?"0"+minute:""+minute;
var str_second = second<10?"0"+second:""+second;
//将这三个时分秒显示到div中指定位置
}
展开全部
<div id="num"></div>
<SCRIPT language="javascript">
secs = 60; // 设置多少秒
wait = secs * 100;
document.getElementById("num").disabled =true;
for(i=1;i<=(wait/100);i++) {
window.setTimeout("doUpdate(" + i + ")", i * 100);
}
window.setTimeout("Timer()", wait);
function doUpdate(num) {
if(num == (wait/100)) {
document.getElementById("num").innerHTML = " ";
} else {
wut = (wait/100)-num;
document.getElementById("num").innerHTML = secs+"秒倒计时: " + wut + "";
}
}
function Timer() {
document.getElementById("num").disabled =false;
}
</SCRIPT>
<SCRIPT language="javascript">
secs = 60; // 设置多少秒
wait = secs * 100;
document.getElementById("num").disabled =true;
for(i=1;i<=(wait/100);i++) {
window.setTimeout("doUpdate(" + i + ")", i * 100);
}
window.setTimeout("Timer()", wait);
function doUpdate(num) {
if(num == (wait/100)) {
document.getElementById("num").innerHTML = " ";
} else {
wut = (wait/100)-num;
document.getElementById("num").innerHTML = secs+"秒倒计时: " + wut + "";
}
}
function Timer() {
document.getElementById("num").disabled =false;
}
</SCRIPT>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<div id="test"></div>
<script type="text/javascript">
var times=10;//倒计时时间
var divObj =document.getELmentById("test");
divObj.innerHTML=times;
function jishi(){
var tem =divObj.innerHTML;
if(tem==1){//最后一秒后隐藏
if(typeof(t)!="undefined")//停止计时器
clearInterval(t);
divObj.style.display="none";
}else{
divObj.innerHTML=parseInt(tem)-1;
}
}
var t =setInterval("jishi()",1000);
</script>
<script type="text/javascript">
var times=10;//倒计时时间
var divObj =document.getELmentById("test");
divObj.innerHTML=times;
function jishi(){
var tem =divObj.innerHTML;
if(tem==1){//最后一秒后隐藏
if(typeof(t)!="undefined")//停止计时器
clearInterval(t);
divObj.style.display="none";
}else{
divObj.innerHTML=parseInt(tem)-1;
}
}
var t =setInterval("jishi()",1000);
</script>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询