Canvas.style.display = "block" does not work

I canโ€™t explain why the first line in the "buttonHandler" function below will not work, after this statement the canvas will not be displayed. But the first line of the "startPlay" function is the same, and this displays the canvas! If anyone can understand why from this section of code, please let me know ..

thank.

I tried this on Chrome and Firefox with the same results.

function buttonHandler(){
    canvas.style.display = "block";
    var menuDisplay = document.getElementById("menuDisplay");
    menuDisplay.style.display = "none";

    drawingSurface.font = readyDisplay.font;
    drawingSurface.fillStyle = readyDisplay.fillStyle;
    drawingSurface.textBaseline = readyDisplay.textBaseline;
    drawingSurface.fillText(readyDisplay.text, readyDisplay.x, readyDisplay.y);
    window.setTimeout("startPlay()", 3000);
}

function startPlay(){
    canvas.style.display = "block";
    gameOver.style.display = "none";
    balls=[];
    for(var i=0;i < sprites.length; i++){
        var thisSprite = sprites[i];
        if(thisSprite !== cup){
            removeObject(thisSprite, sprites);
        }
    }
    score = 0;
    totalSeconds = 5;
    gameState = PLAYING;
    drawingSurface.clearRect(0, 0, canvas.width, canvas.height);
    window.setTimeout("tick()", 1000); // Start the countdown timer
    timeToBall = Math.floor(Math.random() * 250) + 50;
    update();
}
+3
source share
1 answer

You are probably running your code inside the event window.onloadon the page.

When you call setTimeoutlike this with a string function:

window.setTimeout("startPlay()", 3000); // just a string reference

the startPlay() window, , startPlay() , onload , , window.

( , )

setTimeout(), , , ( , ).

, :

window.setTimeout(startPlay, 3000); // now we have an actual function reference

( ).

Live working example (almost the same code, only direct function ref.)

Hope this helps!

+2
source

All Articles