Jump to content

SetTimeout: timer_is_on?


tinfanide
 Share

Recommended Posts

Original W3schools scripts:

<html><head><script type="text/javascript">var c=0;var t;var timer_is_on=0;function timedCount(){document.getElementById('txt').value=c;c=c+1;t=setTimeout("timedCount()",1000);}function doTimer(){if (!timer_is_on)  {  timer_is_on=1;  timedCount();  }}function stopCount(){clearTimeout(t);timer_is_on=0;}</script></head><body><form><input type="button" value="Start count!" onclick="doTimer()"><input type="text" id="txt"><input type="button" value="Stop count!" onclick="stopCount()"></form></body></html>

When I was reading the JS tutorial on W3schools, I didn't know what the variable timer_is_on is used for.I tried deleting all it throughout the script and writing something like

<html><head><script type="text/javascript">var c=0;var t;function timedCount(){document.getElementById('txt').value=c;c=c+1;t=setTimeout("timedCount()",1000);}function doTimer(){  timedCount(); }function stopCount(){clearTimeout(t);}</script></head><body><form><input type="button" value="Start count!" onclick="doTimer()"><input type="text" id="txt"><input type="button" value="Stop count!" onclick="stopCount()"></form></body></html>

Without that variable, the clock still does the counting by 1s as expected.I wonder what timer_is_on is used for in the JS script.

Link to comment
Share on other sites

Press the Start button multiple times without pressing Stop and see what happens.
Yes, I'll have to stop the exact times I start the timer in order to stop the number counting.I set the timer_is_on to 2 within the if statement, it seems running the same as when it's set to be 1.And what is the meaning of if (!timer_is_on)? If not timer_is_on?
Link to comment
Share on other sites

Right, the ! character is the "not" operator. It's listed here:http://www.w3schools.com/js/js_comparisons.asp
I've tested changing (!timer_is_on) to (timer_is_on == 0) as I think (!timer_is_on) means if the variable equals 0.If it equals 0, the variable is now set to 1. Then, start timedCount function.When the stop button is clicked, stop the function and return the variable to 0.But to the best of my knowledge, I still cannot understand how it works to make sure the timer will keep itself counting 1 by 1.I saw what would happen without that variable set between 0 and 1 as reminded by justsomeguy.But just poorly, I could not figure out why it is added there.
Link to comment
Share on other sites

0 equates to false, which when checked in an if condition, (which evaluates statements for 'thuthiness') is how it determines whether the timer is on (1) or off (0). So if the timer is off (!timer_is_on) then you can start it. So it will never start more than one timer at a time with this condition check.

Link to comment
Share on other sites

Right, instead of 0 and 1 you can also use true and false. That's what that variable is, just a boolean variable to keep track of whether or not the timer is running. It doesn't need to be a number, any number other than 0 is considered "true". If you leave that variable out and click start twice, it's going to update the counter twice every second instead of once every second. Checking whether or not the timer is already running is what keeps it at once per second instead of more than once per second. The timedCount function is the function which actually runs every second and updates the counter variable.Note that this won't actually be chronologically precise, it's not going to run the function exactly once every second. If you leave it running for long enough and compare it with a stopwatch you'll see that eventually it shows the wrong time. It's a useful example, but it takes more code than that to actually make it show the correct number of elapsed seconds since you started the timer.

Link to comment
Share on other sites

Right, instead of 0 and 1 you can also use true and false. That's what that variable is, just a boolean variable to keep track of whether or not the timer is running. It doesn't need to be a number, any number other than 0 is considered "true". If you leave that variable out and click start twice, it's going to update the counter twice every second instead of once every second. Checking whether or not the timer is already running is what keeps it at once per second instead of more than once per second. The timedCount function is the function which actually runs every second and updates the counter variable.Note that this won't actually be chronologically precise, it's not going to run the function exactly once every second. If you leave it running for long enough and compare it with a stopwatch you'll see that eventually it shows the wrong time. It's a useful example, but it takes more code than that to actually make it show the correct number of elapsed seconds since you started the timer.
Thanks for you two's explanation. 0 = false, >=1 = true. After knowing this, I find it not that difficult.Yes, it does not exactly equal a second.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...