Search the Community
Showing results for tags 'global variables'.
-
Hi, I tried a couple of searches and couldn't find a post that compared these 2 forms of storage. I was using sessionStorage in my script to remember information from a Db, but as I was reviewing variable properties and scope, I said to myself Why should I involve sessionStorage when I can just assign the info to a global variable which is accessable to the whole website and goes away when you leave the website, just like a session variable. Is this correct? Are the global variables not as reliable or something? What are the differences, similarities; and what can you recommend to me about this. Thanx a million!
-
The last example for window.clearTimeout() method has an explanation that appears to be inconsistent with the 'try it yourself' example on the JavaScript Timing Events page. I have put the text needing corrected in red: How to Stop the Execution?The clearInterval() method is used to stop further executions of the function specified in the setInterval() method. Syntaxwindow.clearInterval(intervalVariable) The window.clearInterval() method can be written without the window prefix. To be able to use the clearInterval() method, you must use a global variable when creating the interval method: myVar=setInterval("javascript function",milliseconds); Then you will be able to stop the execution by calling the clearInterval() method. ExampleSame example as above, but we have added a "Stop time" button: <p id="demo"></p><button onclick="myStopFunction()">Stop time</button><script>var myVar=setInterval(function(){myTimer()},1000);function myTimer(){var d=new Date();var t=d.toLocaleTimeString();document.getElementById("demo").innerHTML=t;}function myStopFunction(){clearInterval(myVar);}</script> Try it yourself ยป If I have been learning my lessons correctly, typing in var makes the variable local. If this is the case, then it would seem the clearInterval() worked without setting a global variable. Does this need updated?
- 3 replies
-
- JavascriptTiming Events
- window.clearTimeout()
- (and 7 more)