我使用SetInterval();
函数来显示时间,有时它停止它不是常规行为,它只是停止。
以下是我的代码:
<script language="JavaScript">
setInterval(settime, 1000);
function settime()
{
try
{
var curtime = new Date();
var curhour = curtime.getHours();
var curmin = curtime.getMinutes();
var cursec = curtime.getSeconds();
var time = "";
if(curhour == 0) curhour = 12;
time = (curhour > 12 ? curhour - 12 : curhour) + ":" +
(curmin < 10 ? "0" : "") + curmin + ":" +
(cursec < 10 ? "0" : "") + cursec + " " +
(curhour > 12 ? "PM" : "AM");
var currentTime = new Date()
var month = currentTime.getMonth();
var year = currentTime.getFullYear()
var day = currentTime.getDay();
var dayString = "";
if(day == 0)
dayString = "Sunday";
if(day == 1)
dayString = "Monday";
if(day == 2)
dayString = "Tuesday";
if(day == 3)
dayString = "Wednesday";
if(day == 4)
dayString = "Thursday";
if(day == 5)
dayString = "Friday";
if(day == 6)
dayString = "Saturday";
var fullDate = day + "/" + month + "/" + year;
document.date.clock.value = "Date : "+fullDate;
document.date.clock1.value = "Time : " + time;
}
catch(err)
{
alert("Errorr from Function");
}
}
当有setInterval();
停止并重新开始时,有没有办法捕捉!!
答案 0 :(得分:0)
setInterval是众所周知的问题。改为使用setTimeout的递归调用。
http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/