① js setInterval循環問題
你在函數內再次用setInterval調用函數本身,就會產生疊加效果,速度會越來越快。應該一次性用setInterval調用函數即可:
<script>
vari=0;
vartimer=setInterval(function(){
document.getElementById("text").value=i++;
},5000);
functionstop2(){
clearInterval(timer);
}
</script>
或者改用setTimeout也可,這是一次性調用的(相當於定時器),那麼在函數內再次調用就能實現循環效果了。而setInterval本身就是重復調用的(相當於計時器),不能放在函數內使用:
<script>
vari=0;
vartimer=0;
window.onload=functionstart2(){
document.getElementById("text").value=i++;
timer=setTimeout(start2,5000);
}
functionstop2(){
clearTimeout(timer);
}
</script>
② 求24小時時間JS代碼
<scriptlanguage="javascript">
<!--
//以下是提示顯示
functionshowclock()
{
varoutStr="";
vardate=newDate();
varhour=date.getHours();
varmin_=date.getMinutes();
varsec=date.getSeconds();
varcol=":";
varspc="";
//時分秒小於10的時候在前面添加個0
if(hour<10)hour="0"+hour;
if(min_<10)min_="0"+min_;
if(sec<10)sec="0"+sec;
outStr=hour+col+min_+col+sec+spc;
document.getElementById("clock").innerHTML=outStr;
}
setInterval("showclock()",1000);
document.write('<spanid="clock"></span>');
//-->
</script>
③ 請javascript高手幫寫一個,天數自動加一的程序,每24小時就加一天,天數沒有上線
<html>
<body>
<span id="safeD" style="width:200"></span>
<span id="safeH" style="width:200"></span>
<span id="safeM" style="width:200"></span>
<span id="safeS" style="width:200"></span>
<div id="times"></div>
<script type="text/javascript">
var startTime = new Date(2010,01,01,00,00,00);//初始時間 2010-1-1 00:00:00
var year;
var month;
var day;
var hour;
var minute;
var seconds;
var safeDay;
var safeHour;
var safeMinute;
var safeSeconds;
setInterval("addTime()",1000); //反復調用
function addTime()
{
var timeNow = new Date();
safeDay = parseInt((timeNow.getTime()-startTime.getTime())/(1000*60*60*24));
safeHour = parseInt((timeNow.getTime()-startTime.getTime())/(1000*60*60));
safeMinute = parseInt((timeNow.getTime()-startTime.getTime())/(1000*60));
safeSeconds = parseInt((timeNow.getTime()-startTime.getTime())/1000);
year = timeNow.getFullYear();
month =timeNow.getMonth()+1;
day = timeNow.getDate();
hour = timeNow.getHours();
minute = timeNow.getMinutes();
seconds = timeNow.getSeconds();
document.getElementById("safeD").innerHTML="安全生產天:"+safeDay;
document.getElementById("safeH").innerHTML="安全生產時:"+safeHour;
document.getElementById("safeM").innerHTML="安全生產分:"+safeMinute;
document.getElementById("safeS").innerHTML="安全生產秒:"+safeSeconds;
document.getElementById("times").innerHTML="當前時間:"
+year+"年"+month+"月"+day+"日"+hour+"時"+minute+"分"+seconds+"秒";
}
</script>
</body>
</html>