Change the color by setTimeout

What am I doing wrong that you click the “click” the color changes only once?



<p id="klik">Click</p>
<div id="block">example text change color</div>

<script>
document.getElementById('klik').onclick=function(){
  setTimeout('ppp()', 1000);  
}
function ppp() {
      var index = Math.round(Math.random() * 3);
      var ColorValue = "green";
      if(index == 1) ColorValue = "red";
      if(index == 2) ColorValue = "lime";
      if(index == 3) ColorValue = "yellow";
      document.getElementById("block").setAttribute("style","color:" + ColorValue);  
} 
</script>

Hi there,

To have an action repeat indefinitely use setInterval()

document.getElementById('klik').onclick=function(){
  setInterval('ppp()', 1000);  
}

This’ll work.

You can read more about the differences here: http://javascript.info/tutorial/settimeout-setinterval