① 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>