Im using SetInterval();
function to display time ,and sometimes it stop's it's not a regular behavior ,it just stops .
Here below is my code :
<script language="JavaScript">
setInterval(settime, 1000);
function s开发者_运维百科ettime()
{
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");
}
}
So is there a way how to catch ,when setInterval();
stops and to start it again !!
setInterval is notoriously problematic. Use recursive calls to setTimeout instead.
http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/
精彩评论