mercredi 3 mars 2021

for loop is broken in JavaScript or i am just dumb i cant run if else properly inside for loop

the following code is just to test the functionality

for (var i = 1; i <= 5; i++) {

  if(i==4){
    console.log(document.getElementById("loading").classList.contains("invisible"));
    break;
  }else{
    (function(i){ 
    setTimeout(function() { console.log(i) }, i*1000);
    })(i);
  }
  
}
/*this basically gives true or false as result
document.getElementById("loading").classList.contains("invisible")*/

this is the console log

false
1
2
3

now my question is how & why am I getting false even if the if condition "(1==4)" is not true and why doesn't the code break when the first part of if statement is executed to get false

Aucun commentaire:

Enregistrer un commentaire