4

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?

1
  • You want to call variable? How? Are variables hiding something from us?! Do variables have own methods? Commented Apr 19, 2017 at 16:21

5 Answers 5

6

setInterval() does the cyclic calls in itself(see edit) and returns the ID of the process handling the cyclic invokations. The purpose of assigning the return value is to use clearInterval() afterwards since it requires you to pass the return value of a setInterval() (= the process ID) as its parameter.

In your case, if you want the function you passed in setInterval() to not be called again (by the "cycling call chain" you created using setInterval) you can simply do clearInterval(refresh).

EDIT

setInterval needs two parameters : an inline function (or a function pointer) and an integer.

What setInterval does is wait {integer passed} milliseconds and then calls the function and redo the same thing over and over until you call clearInterval passing setInterval's return value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response! I was just a bit confused if this was a trait of setinterval() itself or something to do with javascript assignment. Makes more sense now. Thanks again!
3

setInterval will schedule the recurring execution of a function expression/reference passed as its first argument and return an unique identifier for this scheduling. The passed function will be invoked each X milliseconds (this interval is the second argument passed to setInterval). You don't need to assign its return value to a variable in order to use setInterval. The main reason to store this identifier is to cancel this scheduling afterwards, by calling clearInterval(yourIntervalIdHere).

Comments

1

setInterval will immediately kick off the timers to run the function. The function simply returns an ID to track the timer to cancel it later. See the documentation below.

https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/timers#setInterval(callback_ms)

Comments

1

myVar = setInterval(alertFunc, 3000);

this is not storing the function variable to myVar, this store the output of the "setInerval" function to the myvar.

for simpler example , in the below code you are assigning the return of testfunc() to test.

var test;
function foo(){
  test = testfunc();
}
function testfunc(){
  console.log("hi");
}
foo();

if you want to assign testfunc function to test you have to write like

var test;
function foo(){
  test = testfunc;
}
function testfunc(){
  console.log("hi");
}
foo();

then you need to call explicitly

2 Comments

Okay this makes sense. Just to clarify, in the first code snippet you have above, testfunc() would immediately run when foo() is called and store that output into "test" whereas in the second code snippet the "testfunc" function would not be called until you explicitly call test()?
exactly . In java script function are object so you can call a function and get the output and store (1st snippet) or you can directly store the function in the variable (2nd snippet)
-1

It doesn't "execute the setInterval method without an explicit call". The explicit call is right here: myVar = setInterval(alertFunc, 3000); inside the function myFunction.

myvar contains the return of calling setInterval.

7 Comments

wow a down vote for a correct answer. how cute. Notice the "Essentially what I'm asking is why does the "myVar" variable below execute the setInterval method without an explicit call?"
are you kids fighting again by downvoting each other's answers?
Also, technically your answer is not correct. It does not execute the function right away (at least that's what you answer implies), since it is only handing over a function reference which is executed later.
Excuse me? this setInterval() is a function call. Notice the parenthesis? This is how we can tell it's a function call. From the very first line on this page: developer.mozilla.org/en-US/docs/Web/API/… "The setInterval() method".
Yes, but the function you hand over as parameter does not get executed right away! It is simply added to the event loop and executed later (which is also, why the OP was confused).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.