我想首先获取计时器,然后查看结果。当我打开页面并启动函数war()
它显示结果时,它会显示设置颜色的冷却时间。我想首先获得冷却时间,然后是结果。
JavaScript的:
var points = 100;
function war() {
var dealer = document.getElementById("dealer");
var player = document.getElementById("player");
var winner = document.getElementById("winner");
var winner1 = document.getElementById("winner");
var screen = document.getElementById("points");
var dealercard = Math.floor(Math.random() * 10) + 1;
var playercard = Math.floor(Math.random() * 10) + 1;
dealer.innerHTML = dealercard;
player.innerHTML = playercard;
var skaiciuoklis = 5;
var naujaselemnt = document.createElement("p");
naujaselemnt.innerHTML = "Wait... 5 seconds.";
var idwar;
dealer.parentNode.replaceChild(naujaselemnt, dealer);
idwar = setInterval(function() {
skaiciuoklis--;
if (skaiciuoklis < 0) {
winner1.parentNode.replaceChild(dealer, naujaselemnt);
clearInterval(idwar);
} else {
winner1.innerHTML = "Wait... " + skaiciuoklis.toString() + " seconds.";
}
}, 500);
if (dealercard > playercard) {
winner.style.color = "red";
winner.innerHTML = "You lost 20 points";
} else {
winner.style.color = "green";
winner.innerHTML = "You won 10 points!";
}
if (dealercard == playercard) {
winner.style.color = "blue";
winner.innerHTML = "WAR. You won 20 points!"
}
if (dealercard < playercard) {
points = points + 10;
screen.innerHTML = points;
}
if (dealercard == playercard) {
points = points + 20;
screen.innerHTML = points;
}
if (dealercard > playercard) {
points = points - 20;
screen.innerHTML = points;
}
if (points <= 0) {
winner.style.color = "red";
winner.innerHTML = "Game Over";
alert("Game Over. REFRESH PAGE TO RETRY!");
points = 100;
}
答案 0 :(得分:0)
您可以使用ES6异步等待并将您的计时器移动到外部的单独功能。你有很多变量而且它不是那么清楚,但看起来你的计时器在函数war()中。 Javascript是异步的,即使你有一个计时器它也会移动到下一个代码。
这样的例子:
function timer(){
// implement timer
}
async function war() {
// at the line u want to call the timer
await timer();
}