mercredi 20 mai 2015

Use setTimeout and if to control objects (Javascript)

I encounter a problem when practicing Javascript. I'd like to make a square that I can click it. After clicking, it disappears and shows after 3 seconds. And the code is as below.

    document.getElementById("box").addEventListener("click",function(){
    this.style.display="none";
    });


    if (document.getElementById("box").style.display=="none"){
    setTimeout(function(){
        document.getElementById("box").style.display="block";
    }, 3000);}

Here's a JS Bin
So how to correct the code above? It seems to be something wrong in setTimeout.

Aucun commentaire:

Enregistrer un commentaire