I'm having a hard time understanding the logic behind the setTimer method in javascript.
<html><head>
<script>
function Timer () {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
document.getElementById('show').innerHTML=h+":"+m+":"+s;
t = setTimeout("Timer()", 1000);
}
</script>
</head>
<body onload="Timer()">
<div id="show"></div>
</body></html>
setTimeout is used to delay a function/method execution. Then why it is being used in a real-time clock?
t = setTimeout("Timer()", 1000);
This part is confusing.
I'm having a hard time understanding the logic behind the setTimer method in javascript.
<html><head>
<script>
function Timer () {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
document.getElementById('show').innerHTML=h+":"+m+":"+s;
t = setTimeout("Timer()", 1000);
}
</script>
</head>
<body onload="Timer()">
<div id="show"></div>
</body></html>
setTimeout is used to delay a function/method execution. Then why it is being used in a real-time clock?
t = setTimeout("Timer()", 1000);
This part is confusing.
Share Improve this question edited Sep 16, 2012 at 10:47 David Hall 33.2k10 gold badges92 silver badges129 bronze badges asked Sep 16, 2012 at 7:20 user1666411user1666411 3352 gold badges7 silver badges11 bronze badges4 Answers
Reset to default 2The clock is recursively calling itself, after the elapsed period of time.
Making a real-time clock is impossible in JS.
Because of how JS engines work, if you put Timer
in a loop, to run for an infinite period of time, you'd never see the time update on the screen (as changes aren't drawn to the window until a function finishes and there's a gap in the program).
Also, inside that infinite-loop, it would be impossible to do anything else with the page (even closing it), because JS can only do one thing at a time, so it can't listen to any of the user's clicking until it's done with this loop.......
So that's what the setTimeout
is for.
Timer
is the function which acts as the clock.
Inside of the Timer
function, at the end when all of the work is done, it's telling setTimeout
to wait 1 second (1000ms) and then to call a function called Timer
.
Timer
just so happens to be the same function. But setTimeout
doesn't know that, and doesn't care.
The t
in this case is largely useless. setTimeout
will return a number -- like taking a number at the doctor's office.
If, before you go through with it, you decide to back out, you can call clearTimeout(t);
and it'll skip over that call (in this case, it would stop calling the clock).
There are a few bad-practices in here, that I figure I should mention, so that you can try not to copy them in your own practice.
First:
Pass setTimeout
a reference to a function, and not a string...
var runThisFunction = function () { console.log("It's the future!"); },
time_to_wait = 250;
// DON'T DO THIS
setTimeout( "runThisFunction()", 250 );
// DO THIS
setTimeout( runThisFunction, 250 );
The difference is that setTimeout
will run that string through eval
, which can be a huge security concern depending on what you're trying to do.
The second problem is setting a random global variable, t
... ...and hoping to use that as a solution.
First, in a couple of years, JS engines are going to start yelling at people for doing that stuff. Second, it's a huge hole, because any part of any app on that page could then overwrite t
, or you could be relying on t
somewhere else in your script, but every 1000ms, it gets written over with a new number.
Instead, they probably should have used a Timer.start();
and Timer.stop();
setup.
Your code:
t = setTimeout("Timer()", 1000);
The first thing you should know is that it's considered bad practice to put the first parameter in a string -- it should be the function name, unquoted, and without brackets, like so:
t = setTimeout(Timer, 1000);
That aside, your question about why it's being used to display a clock:
The use of setTimeout()
inside the Timer()
function to call itself is a mon Javascript pattern to get a function to be called repeatedly. setTimeout()
itself only triggers the function to be called a single time, after the given period of time has elapsed, so for a repeating event it needs to be re-triggered every time.
Since the setTimeout
call is inside the Timer()
function, it won't be set until Timer()
is called the first time by some other means. This is where the body onload
es in.
As you suspect, setTimeout()
isn't an accurate method for guaranteeing that a function will be called after exactly a given amount of time. Javascript is not multi-threaded, so any event handlers that are triggered must wait for any other code that is running at the same time. If something else is running slowly, this may cause your timer not to be triggered at exactly the moment it wants to be.
However, this isn't really a problem for your clock , because the clock is setting itself to the actual system time rather than relying on the setTimeout
loop to keep itself in sync; the setTimeout
loop is simply being used to make sure the display is updated (approximately) once a second. If it isn't actually quite exactly once a second, it doesn't really matter.
I hope that helps explain things a bit better.
When the Timer()
function is called, it schedules itself to be run again one second later. The end result is once every second, Timer()
updates the show
element with the current time. (I have no idea why it's assigned to t
, unless t
is used in some other code on the page.)
The line starts The function again after one second.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745467011a4628959.html
评论列表(0条)