在javascript中制作等待功能

时间:2019-01-26 17:49:00

标签: javascript

function showWinner(Winner){
    if (Winner === "user"){
        winner.innerHTML = `You Win!`;
    } else if (Winner === "computer") {
        winner.innerHTML = `You Lose!`
    } else {
        winner.innerHTML = `Drawn`;
    }
    gameTable.style.display = "none";
    mainScreen.style.display = "none";
    showResult.style.display = "flex";
}

在上面的代码中,我希望函数在更改dom元素的样式之前停止2秒,如何使用纯JavaScript做到这一点。

1 个答案:

答案 0 :(得分:0)

您可以简单地使用setTimeout

setTimeout(showWinner(Winner),2000);

注意时间以毫秒为单位