Sunday, 15 September 2013

HTML body onload() property

HTML body onload() property

I wrote the following functions:
function fill(value) {
return (value > 9) ? "" + value : "0" + value;
}
function fillMili(value) {
value %= 1000;
return (value <= 9) ? "00" + value :
(value <= 99) ? "0" + value :
"" + value;
};
function showClock() {
var now = new Date;
time = fill(now.getHours()) + " : " + fill(now.getMinutes()) + " : " +
fill(now.getSeconds()) + " : " + fill(now.getMilliseconds());
document.getElementById("clock").innerHTML = time;
setTimeout("showClock()",1);
}
And then put the function into the body tag and create a div tag:
<body onload="showClock()">
<div id="clock">
</div>
The page will show a fine clock. But if I change this line of code
setTimeout("showClock()",1);
to
setInterval("showClock()",1);
The browser will freeze on loading this page.
I know that the function setTimeout() will execute the assigned function
once and the function setInterval() will execute the assigned function
several times.
I have 2 questions: how does the onload property of the body work and why
doesn't the function setInterval() work in this code?

No comments:

Post a Comment