0

here are two questions about the exercise that I am doing for practicing of setInterval fuction

  1. from following code, I tried to print out the variable which is self-adding by 10 within 1000 milliseconds, but when I ran it, in the console of browser, it only showed 10 times of same word "num", how do I figure it out ?

$(function() {
  var timmer;
  GoCount();

  function GoCount() {
    timmer = setInterval(function() {
      var num = 0;
      num += 10;
      console.log(num);
    }, 1000);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  1. and the second question is what's the difference between setInterval and for loop
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 9
    Move `var num = 0;` out of the GoCount function, and then it will work just fine. – Carsten Løvbo Andersen Jan 08 '20 at 08:47
  • 2
    2. `setInterval` iterates at a given delay and is effectively asynchronous. A `for` loop runs synchronously without delay (unless once is manually created). If you're ever in doubt about anything to do with JS, always refer to MDN: [`setInterval()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval), [`for` loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) – Rory McCrossan Jan 08 '20 at 08:49
  • This does *not* output *the word "num"*…! – deceze Jan 08 '20 at 08:57
  • @Carsten Løvbo Andersen , it works ! many thanks ! – Josh Caffery Jan 08 '20 at 09:01

6 Answers6

3

As @Carsten Løvbo Andersen 's comment, you should make the num as global variable to be able to keep the previous value.

$(function() {
  var timmer;
  var num = 0;
  GoCount();

  function GoCount() {
    timmer = setInterval(function() {
      num += 10;
      console.log(num);
    }, 1000);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
1

Each time you the function runs you set "num" back to zero and add 10. You need to declare the num as 0 outside of that function, in the same place as "timmer".

heraphim
  • 326
  • 2
  • 10
1

You can pass your variable as function params then you can change the start number as well.

$(function() {
  var timmer;
  GoCount(0);

  function GoCount(num) {
    timmer = setInterval(function() {
     
      num += 10;
      console.log(num);
    }, 1000);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

codes inside a loop will be run without delay, but in setInterval you can set that how much time should be between running the codes in your scope.

Nasser Ali Karimi
  • 4,462
  • 6
  • 34
  • 77
1

Create the num variable outside the setInterval function. In your code the num variable is declare as new variable and assigned the value to 0 each time the setInterval executes.

$(function() {
    var timmer;
    GoCount();

    function GoCount() {
        var num = 0;
        timmer = setInterval(function() {
            num += 10;
            console.log(num);
        }, 1000);
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Sohail Ashraf
  • 10,078
  • 2
  • 26
  • 42
1

You can make your code simpler by creating an IIFE like this:

((num) => setInterval(() => {
  num += 10
  console.log(num)
}, 1000))(0) // <- Initialize the `num` variable here
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

If you want to increase the num by 10 with each iteration you need to move out the num variable (as @Carsten Løvbo Andersen said):

$(function() {
  var num = 0;
  var timmer;
  GoCount();

  function GoCount() {
    timmer = setInterval(function() {
      num += 10;
      console.log(num);
    }, 1000);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

See https://jsfiddle.net/f8hn4xrw/ for a working example

adiga
  • 34,372
  • 9
  • 61
  • 83
Mario Murrent
  • 712
  • 5
  • 24