'print number from 1 to 10 after every 2 seconds

i want to print number after every n number of seconds and based on few conditions i am changing the timer as well as i am stopping the print function. i have done like this --

var myfunc = {
    value   : 1,
    running : false,
    timer   : 1000,
    start   : function(){
        this.running = true;
        clearInterval(this.timeout);
        this.timeout = setTimeout(function() {
            myfunc.execute(myfunc);
        }, myfunc.timer);

    },
    execute : function(){
        if(!this.running) return false;

        console.log( 'Currently at -- ' + (this.value++) );

        if (this.value > 5 ){
            this.changetiming();
        }

        if (this.value > 10 ){
            this.stop();
            return;
        }else{
            this.start();
        }

    },
    changetiming : function(){
        this.timer = 3000;
    },
    stop : function(){
        this.running = false;
        clearTimeout(this.timeout);
    }
};

myfunc.start();

Now i want to know what is wrong with following code --

for(var i = 0; i <= 10; i++){
    print(i);
}


function print(i){
    setTimeout(function(){
        console.log(i)
    },2000);
}


Solution 1:[1]

here is the right way and easy way to do this in ES6+:

const printNumbersForEvery2Sec = (n)=>{
  for (let i = 1; i <= n; i++) {
      setTimeout( () =>{
        console.log(i)
      }, i * 2000)
    }
}
printNumbersForEvery2Sec(10);

by multiplying i , each setTimeout() to be delayed 2 to 20 seconds (2000 x 1, 2000 x 2…) respectively.

Solution 2:[2]

I'm pretty sure the question "Why does this JavaScript code

for (var i = 0; i <= 10; i++){
    print(i);
}

function print(i) {
    setTimeout(function(){
        console.log(i)
    },2000);
}

print out the values 1 through 10 at once, after 2 seconds have elapsed?" has been asked before.

It is a common mistake.

What you are doing is calling print 10 times. Each call to print takes only a few microseconds. Why? Because it just calls setTimeout. Executing setTimeout takes only a few microseconds to complete. All the call does is schedule something to take place in the future. So within a few microseconds you have scheduled 10 things to take place at about 2 seconds in the future. All the schedulings happen at about the same time. So all the console logs take place at about the same time, two seconds after you scheduled them.

See Satapal's comment to your question for a nice way to do what you want to do.

Solution 3:[3]

#easiestway

for (var i = 0; i <= 10; i++){
    print(i);
}

function print(i) {
    setTimeout(function(){
        console.log(i)
    },i*2000);
}

Solution 4:[4]

for(var i = 0 ; i <= 10 ; i++) {
  setTimeout( () => { console.log(i) }, i * 1000 );
}

Why we are not able to print 0 to 10 no with the help of var? Why we are able to do with let?

Solution 5:[5]

You might want to try this:

const printNumbersForEvery2Sec = (n)=>{
  for (let i = 1; i <= n; i++) setTimeout(console.log, i * 1000,i) 
}

printNumbersForEvery2Sec(10);```

Solution 6:[6]

You can use timeout of javascript

function timer(n) {
    for (let i = 0; i < 10; i++) {
        setTimeout(function () {
            console.log(i);
        }, i * n);
    }
}

timer(2000);

In the above code timing is not coded so you can decide how much interval you want.

Solution 7:[7]

A more generalised solution:

function printNumbers(start, end, delay=1){
  const interval = delay*1000
  for(let i=start; i<=end; i++){
      setTimeout(console.log, (i-start)*interval, i)
  }
}

printNumbers(3, 10, 2) // firstNumber, lastNumber, timeInSeconds

Solution 8:[8]

Using IIFE, Clouser, and Global Scoping

(function(numbers){
  for(var i=0; i<numbers.length; i++){
    (function(i){
      setTimeout(console.log, i*2000, i+1)
    })(i);
  }
})(new Array(10))

OR IIFE and Local Scoping

(function(numbers){
  for(let i=0; i<numbers.length; i++){
    setTimeout(console.log, i*2000, i+1)
  }
})(new Array(10))

Solution 9:[9]

for (let i = 1; i <= 10; i++) {
  setTimeout(() => {
    console.log(i)
  }, (i * 2000) )
}

Far best! and easy solution... Checkout snippet...

for (let i = 1; i <= 10; i++) {
  setTimeout(() => {
    console.log(i)
  }, (i * 2000) )
}

Solution 10:[10]

const RandomUnderHundredNumber = (min,max) => {
min = Math.ceil(min);
max = Math.floor(max); 
return Math.floor(Math.random() * (max - min + 1)) + min; /*min and max numbers are included */
}
let inter
const testFunction = () => {
inter = setInterval(() => console.log(RandomUnderHundredNumber(0,99)), 1000) // generate number between 0 and 99 every 1 sec  
}
testFunction();
setTimeout(function( ) { clearInterval(inter); }, 5000); /* clear interval after 5 sec delay (optional) */

Solution 11:[11]

const printNumbersForEvery2Sec = (n)=>{
  for (let i = 1; i <= n; i++) {
      setTimeout( () =>{
        console.log(i)
      }, i * 2000)
    }
}
printNumbersForEvery2Sec(10);