0

I suddenly feel so confused about this example:

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_setinterval_clearinterval

in the script,

var myVar = setInterval(myTimer, 1000);

function myTimer() {
  var d = new Date();
  var t = d.toLocaleTimeString();
  document.getElementById("demo").innerHTML = t;
}

function myStopFunction() {
  clearInterval(myVar);
}

for the

var myVar = setInterval(myTimer, 1000);

Why this setInterval(myTimer,1000) function is executed when we are assigning this whole function to the variable myVar? So if we just use setInterval(myTimer, 1000);, it will be executed, but if we use var myVar = setInterval(myTimer, 1000);, it will be both executed and assigned to the variable myVar at the same time right? May I know what is logic behind it? What is this myVar after this assigning? What is the inner logic that we use clearInterval to this myVar to stop? Thank you so much!

Ying
  • 233
  • 3
  • 16
  • 3
    `myVar` is a number (a timer id) – Jonas Wilms May 01 '19 at 17:44
  • 1
    myVar is used just to hold the reference to the setInterval, so you can handle it later, in this case to clear the interval. – Hosar May 01 '19 at 17:45
  • 1
    It executes because you are calling the function – Isaac Vidrine May 01 '19 at 17:45
  • 1
    The function is being executed only after the specified number of miliseconds. Assigning the interval to a variable has nothing to do with it. – Mitya May 01 '19 at 17:49
  • 1
    When you call a function, like `setTimeout`, this function returns a value. If not explicitely, then it implicitely returns `undefined`. What you do with that returned value, passing it to another function, doing some operations, writing it to a variable or completely ignoring it, that is up to you. – Thomas May 01 '19 at 17:51
  • Thank you guys so much! So the calling of setInterval(myTimer, 1000); will just execute the function, and the assignment just assign the timer id to the myVar? I console log it and find it's 1, so it's just the id of this timer right? So that we can use this id to ask clearInterval to stop the timer with this id 1? – Ying May 01 '19 at 17:53
  • 1
    Yes, you're seeing the value of the timer. As has been stated, assigning intervals or timeouts to variables is almost always used only to later clear them. Also, on a point of clarity, don't think that the line mentioned executes the function - that's wrong; it cues up the function to be executed, after 1000ms. – Mitya May 01 '19 at 17:59
  • @Utkanos Thank you so much!! So var myVar = setInterval(myTimer, 1000); this line actually execute myTimer function the first time after 1000ms then myVar got the id after it's executed right? Then every 1000ms? – Ying May 01 '19 at 18:04
  • The line queues up the interval. It does nothing initially. Only 1000ms later does the fucntion fire, for the first time. Then every 1000ms after that. The line assigns a timer reference (of the interval) to the variable. It's important to know that the line does not invoke the function; it merely queues it up to be invoked. – Mitya May 01 '19 at 20:17

0 Answers0