0
For example, if we keep one setInterval()
in a variable, it already runs automatically even without that variable being called anywhere?
Example:
let i = 0;
let myInterval = setInterval(function(){
console.log(i++)
if(i > 10){
clearInterval(myInterval)
}
},1000)
I didn’t even call the myInterval
nowhere and he’s already running...
Variables are already automatically executed when declared?
The real question is just because in function we have to call the same somewhere, you know?
This is only for JS or for all languages?
For all, it is the behavior of the function. What you assigned to the variable was not the function, it was the return function; and to get the return, the function will be executed.
– Woss
As indicated in documentation the return of
setInterval
is a numerical value that identifies the timer created so that it can later turn it off through the functionclearInterval
. If you don’t want to turn off the timer then you don’t need to capture the return either.– Isac