jeudi 8 juin 2017

Changing text and color randomly

I try to write a code that puts on the screen random texts and change the color sometimes( in loop 2).

I don't understand why my code loops just once. It seems that the show() function works by itself but the test() loop doesn't run well.

<script type="text/javascript">//<![CDATA[
window.onload=function(){

button.addEventListener('mousedown', function (e) {

test();

});

}//]]> 

function show()
{
b=makeid();
document.getElementById("text").innerHTML = b;

}  


function test()
{
var div = document.getElementById('test');

for (i=0;i<5;i++) {b=makeid();
                document.getElementById("text").innerHTML = b;
                if (i==2) {div.style.color = '#000000';}
                setTimeout(function(){ show();},500);
                if (i==2) {div.style.color = '#CC0000';}
                }


}

function makeid()
{
 var text = "";
 var possible =     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 2; i++ )
    text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}

</script>

</head>

<body>
<button id="button">click</button>
<div>
<span id="text"></span>
</div>




Aucun commentaire:

Enregistrer un commentaire