I am writing a simple Coin Flip project to hone my JS skills.
I have a <div id="winMessage"> and I want it to be hidden if I don't click the button for a few seconds. How can I achieve something like this?
I have tried using style.visibility and sleep() to hide/show it between lines but it seems so inefficient and causes lots of problems on other parts.
let heads = 0, tails = 0;
let coinFlip = () => {
let flip = () => {
return Math.floor((Math.random() * 2) + 1);
}
let result = flip();
if (result === 1){
heads++;
document.getElementById("winMessage").innerHTML = "Heads!"
}
else if (result === 2) {
tails++;
document.getElementById("winMessage").innerHTML = "Tails!"
}
document.getElementById("heads").innerText = heads;
document.getElementById("tails").innerHTML = tails;
}
<head>
<title>Coin Flipper</title>
<link rel="stylesheet" href="style.css" class="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous">
</head>
<body class="bg-dark text-warning" style="margin: auto;">
<header>
<h1>Coin Flip</h1>
</header>
<form>
<div>
<button type="button" class="btn btn-warning" onclick="coinFlip()" style="width: 100%;">Flip It!</button>
</div>
</form>
<div id="winMessage" style="float: center; text-align: center; font-size: 100px; padding-bottom: 10px;"></div>
<div style="float: center; text-align: center;">
<div>Heads Count: <div id="heads" style=></div></div>
<div>Tails Count: <div id="tails"></div></div>
</div>
</body>
```
Use setTimeout() and clearTimeout():
const hide = setTimeout() in main scope, with your delay as a second argumentclearTimeout(hide) on the button clickelement.style.display = "none" to hide the element.hide() insteadlet heads = 0, tails = 0;
let coinFlip = () => {
clearTimeout(hide);
let flip = () => {
return Math.floor((Math.random() * 2) + 1);
}
let result = flip();
if (result === 1){
heads++;
document.getElementById("winMessage").innerHTML = "Heads!"
}
else if (result === 2) {
tails++;
document.getElementById("winMessage").innerHTML = "Tails!"
}
document.getElementById("heads").innerText = heads;
document.getElementById("tails").innerHTML = tails;
}
const hide = setTimeout(() => {
document.getElementById('winMessage').style.display = 'none'
}, 3000)
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous">
<header>
<h1>Coin Flip</h1>
</header>
<form>
<div>
<button type="button" class="btn btn-warning" onclick="coinFlip()" style="width: 100%;">Flip It!</button>
</div>
</form>
<div id="winMessage" style="float: center; text-align: center; font-size: 100px; padding-bottom: 10px;">Hides after 3000ms</div>
<div style="float: center; text-align: center;">
<div>Heads Count: <div id="heads" style=></div></div>
<div>Tails Count: <div id="tails"></div></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With