2013-07-25 106 views
0

我用下面的代碼試圖Javascript代碼凍結屏幕時

  1. 顯示卡片遊戲的結果設定量,而顯示
  2. 等待3秒的倒數,然後,
  3. 重新加載屏幕。

    //Display results, at least that was the idea. 
    for(var player = 0; player < players.length; player++) 
    { 
        resultString = players[player][6] 
        if (result == null) { resultString += ", its a tie."; } 
        else 
        { 
         resultString += (player == 0 ? ", you" : ", cassino") + 
         (player == result ? (player == 0 ? " win" : " wins") : 
         (player == 0 ? " lose" : " loses")); 
        document.getElementById(player == 0 ? "player" : "casino"). 
        childNodes[0].nodeValue = resultString; 
        } 
        showHand(players[player]); 
    } 
    
    //Lag for 3 seconds while displaying a countdown. At least it lags alright. 
    var time = new Date().getTime(); 
    while (((time/1000) + 3) - (new Date().getTime()/1000) > 0) 
    { 
        document.getElementById("call").childNodes[0].nodeValue = "Next round in " 
        + Math.ceil(((time/1000) + 3) - (new Date().getTime()/1000)) 
        .toFixed(0); 
    } 
    //Reload screen. Working fine. 
    dealHands(); 
    } 
    

的問題是,通過運行該代碼的屏幕而不是凍結用於加載重新加載屏幕,即使在被顯示的console.log指出的價值觀沒有問題之前3秒程序的「//顯示結果」部分。實際上,如果我在評論「//滯後3秒...」之後評論部分,結果將按預期顯示。 有沒有.time()的某些屬性,我不知道這是重寫代碼?

回答

0

如果你想在3秒後顯示倒計時,你可以把你的代碼重新加載到一個函數的屏幕上,並使用window.setTimeout來調用該函數,它不會凍結窗口,並且在指定的時間量之後調用函數那裏。

對於倒計時顯示,您可以使用window.setInterval