I have what seems like a basic javascript question that I can't wrap my head around. Why does the below code snippet work (taken from w3 schools)?
Essentially what I'm asking is why does the "myVar" variable below execute the setInterval method without an explicit call? My best guess is that is has to do with the way javascript handles variable assignment?
<html>
<body>
<p>Click the button to wait 3 seconds, then alert "Hello".</p>
<p>After clicking away the alert box, an new alert box will appear in 3 seconds. This goes on forever...</p>
<button onclick="myFunction()">Try it</button>
<script>
var myVar;
function myFunction() {
myVar = setInterval(alertFunc, 3000);
}
function alertFunc() {
alert("Hello!");
}
</script>
</body>
</html>
In case any further clarification is needed, here is a code snippet from my current work:
var refresh = setInterval(function() {
$("#div").load('Query.html');
}, 1000);
So my question is, why does the above work without calling the "refresh" variable elsewhere?