fun with timeouts

by Laurel Bruggeman

JavaScript

console.clear();
var txt = ["a","b","c"];

function works() {
  for (var i = 0; i < 3; ++i ) { 
     setTimeout(( (msg) => { 
        return () => console.log(msg); 
      })(txt[i]), i*1000);         
  }
}

function works1() {
	var counter = 0;
  var c = setInterval(function() {
  	if (counter > 2) { clearInterval(c) }
    else {
      console.log(txt[counter])
      counter++
    }
   }, 1000);  
}

function showCounter() {
  var end = 10;
  var counter = 1;
  var c = setInterval(() => {
  	if (counter > end) {
    	clearInterval(c)
    } else {
    	console.log(counter)
      counter++;
    }
  }, 100)
}

function allC() {
  for (var i = 0; i < 3; ++i ) { 
  	var msg = txt[i]
     setTimeout( (txt) => { 
        console.log(txt[i]); 
      }, i*1000);         
  }
}
showCounter();