Variables are automatically executed when declared?

Asked

Viewed 91 times

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?

  • 5

    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.

  • 1

    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 function clearInterval. If you don’t want to turn off the timer then you don’t need to capture the return either.

3 answers

5


Variables are states and not actions, so it makes no sense to talk about variable execution.

What you did was call the setInterval(), this is enough. As argument passed an action, as he expects. Without the variable works the same way, it is completely useless in this code:

let i = 0;
setInterval(function(){
  console.log(i++);
},1000)

I put in the Github for future reference.

  • In case I put the variable to a possible clearInterval.

  • But, I understood more or less still, even though it was being executed, the value was to stay in the variable and not be executed; Excuse ignorance!

  • but I don’t know it, I can only answer on top of what you posted. You didn’t understand what the setInterval() does then. Probably want to do something else, but that’s also not in the question.

  • Sorry Maniero, it was only to illustrate even that I was saving it. I know it is useless. Just still does not hit the why she executes and not "guard". Anyway, thanks for the answers!

  • I don’t think you read the answer. The variable is useless, it doesn’t have to store anything. It executes anything, like every variable. It executes the function. If it’s not what you want do something else, what’s there beats 100%.

  • I edited the question as I mentioned above, anyway, I still don’t understand; It wasn’t for its bad explanation (Which was very good), it was for my perhaps lack of perception and maturity in JS yet. I know she performs, etc.. But for me, variables serve to store values... Maybe I expressed myself badly.

  • 2

    @Lucascarvalho If you do x = fatorial(5), you will need call for the function x() to obtain the value 120 or the value of x will already be 120?

  • The variable is not useless @Maniero, it is the Handler of your interval. That in case later you can cancel it: clearInterval(myInterval). https://www.w3schools.com/jsref/met_win_clearinterval.asp

  • Allan, before I was without clearInterval. But that’s what I would use, but it’s not Maniero’s fault, I didn’t ask the whole question, sorry!

  • Anderson, now I understand better! What intrigues me is why it shows the result on the console, and I’m not using the variable, I just declared the same.

  • 2

    Precisely because the function setInterval is called to produce the result that will be assigned to the variable. When the function is executed, its effect is that the function passed by parameter is executed at the specified time. Whether or not the variable changes the behavior of the function - that’s what Maniero said, not that it was completely useless (cc @Allan).

  • 4

    @Lucascarvalho, answering your question, when setInterval is called, it already performs such function by initializing the timer. The function returns an Handler to cancel it later. It is the same thing as a function like this: function teste() { console.log("EXECUTEI"); return "valor-qualquer"; }. When you assign the EXECUTION of this function to a variable: var a = teste();, will appear on the console "EXECUTEI" and its variable will have the value "valor-qualquer".

  • Thank you all! With the explanations I understand now! Value @Allan

  • 1

    @Lucascarvalho first, editing the question after being answered is not cool. If you keep doing this, you discourage people from answering your questions because it might invalidate your answer. What changed matters zero, I’m not going to change my answer, but I could change it and go on without, the variable. I don’t know if I can explain it any other way, for me it’s too obvious to try to explain it better. As a friend of mine who teaches at Unicamp says "I can’t explain to the student what a table is"

  • 2

    Perhaps he lacks previous knowledge to understand the difference between each thing, that is to say he is doing something much more advanced than he is capable at this time. It’s not a question of understanding JS, it’s understanding what each thing is in programming, a mistake that I see people make. They want to see results and they don’t bother to actually learn before they do it. I repeated several times that function variable to store values, so I took the variable to show that it is not good for anything, after all you are not saving value there.

Show 10 more comments

0

This is because you have not declared the variable with the function, only the return of the function. Your code should be:

let i = 0;
let myInterval = function(){
    setInterval(function(){
      console.log(i++)
      if(i > 10){
        clearInterval(myInterval)
      }
    },1000)
}

Therefore, only by explicitly calling:

myInterval()

setInterval will be executed.

  • 2

    In this case myInterval will not be the return of setInterval, then it wouldn’t make sense to put it on clearInterval; unless a Return of the return of setInterval

0

You are saving in the variable the function return setInterval, you can store the function this way:

let interval = setInterval

And then you run calling the function name plus the parentheses

interval()

let interval = setInterval
let c = null
let i = 0

let funToExecute = function() {
    console.log(i)
    i++
    if (i > 10) {clearInterval(c)}
}

c = interval(funToExecute, 1000)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.