PDA

View Full Version : Why my browser stuck of this script?



Adas
01-24-2010, 02:23 PM
Can anyone tell me why? Here is the code:

<html>
<head>
<title>clock</title>
<style type="text/css">
<!--
body {
color: #111111;
font-family: Tahoma;
font-size: 13px;
}
a {
text-decoration:none;
color:#222222;
}
a:hover {
text-decoration:underline;
}
-->
</style>
<script type="text/javascript">
clock = {
check:function(a){
return (a<10)? "0"+a : a
},

show:function() {
var time = new Date()
var hours = this.check(time.getHours())
var minu = this.check(time.getMinutes())
var sec = this.check(time.getSeconds())
document.getElementById('time').innerHTML="Now the time is: " + hours + ":" + minu + ":" + sec
setInterval("clock.show()",1000)
},

};
</script>
</head>
<body onLoad="clock.show();">
<div id="time"></div>
</body>
</html>

Philip M
01-24-2010, 02:47 PM
Try this:-



<html>
<head>

<script type="text/javascript">
function show() {
var time = new Date();
var hours = time.getHours();
var minu = time.getMinutes();
if (minu <10) {minu = "0" + minu}
var sec = time.getSeconds();
if (sec <10) {sec = "0" + sec}
document.getElementById('timeofDay').innerHTML="Now the time is: " + hours + ":" + minu + ":" + sec;
setTimeout("show()",1000);
}

</script>

</head>
<body onLoad="show()">
<div id="timeofDay"></div>

</body>
</html>


Avoid duplication of HTML element, function and variable names. (time in this case)
} ,


Quizmaster: The small primates called lemurs are native to which large island off the coast of Africa?
Contestant: Argentina.

Adas
01-24-2010, 03:30 PM
Mh. Thanks. And I should use setTimeout() instead setInterval() :thumbsup: