alzami Posted December 24, 2013 Share Posted December 24, 2013 what is the actual meaning of these statement #if(!var_name) #if(var_name) Link to comment Share on other sites More sharing options...
Ingolme Posted December 24, 2013 Share Posted December 24, 2013 They're equivalent to if(var_name == false) and if(var_name == true) Link to comment Share on other sites More sharing options...
alzami Posted December 24, 2013 Author Share Posted December 24, 2013 (edited) in this example what is the use of the variable (timer_is_on)? http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_settimeout_cleartimeout2 it is not even needed here. what is the use of If(!timer_is_on) in the function startcount()?? Edited December 24, 2013 by alzami Link to comment Share on other sites More sharing options...
Hadien Posted December 24, 2013 Share Posted December 24, 2013 whats happening is that the if() syntax is casting timer_is_on from a number to a boolean. any number other than 0 is considered true. the "!" is a not operator so when javascript sees "if(!timer_is_on)", its roughly translating to "if timer_is_on is NOT 0, then do this stuff" its important that the function made that check so that it didn't make multiple setTimeout function calls. if it did then the id of the previous setTimeout would be lost, overwritten with the new setTimeout function. If that happened then there would be no way to use clearTimeout on the first setTimeout. actually…. after calling stopCount, one of the setTimeouts would clear and then you would be able to stop the 2nd timeout after its made a count, however, that wouldn't be desired functionality and would be confusing. Link to comment Share on other sites More sharing options...
alzami Posted December 24, 2013 Author Share Posted December 24, 2013 thanks!what actually cofused me is i removed the timer_is_on var from the editor and codes worked fine.so i wonder why use an extra var when it is not necessary!! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now