javascript - Why can't orange be shown? -


when change background color of div element every second, orange can't shown.

what doing wrong?

var alldiv = document.getelementsbytagname("div"),      color = ["red", "blue", "yellow", "orange"];    function changecolor(){    for(var = 0; < color.length; i++){      (function(j){        settimeout(function(){          alldiv[0].style.background = color[j];          if(j === color.length - 1){            changecolor();          }        }, 1000 * j);      })(i);    }  }  changecolor();
div {      width: 5rem;      height: 3rem;  }
<div>  </div>

the problem interval in settimeout. orange color might getting set, changed because of new call changecolor - in first iteration, interval settimeout 1000*j = 0.

instead make 1000*(j+1):

settimeout(function() {    ... }, 1000* (j+1)); 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -