-1

I would like to know if I can delay the action of a button in html, so when I click a button, it comes up with an animation or a picture saying; Please Wait... for 1 or 2 seconds, then execute the code? This is a small part of my code that I want to delay;

<button onclick="myFunction()">Summon</button>
<p id="demo"></p>
<script>
function myFunction()
{
    var luck= ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16'],
    lucknumber = Math.floor((Math.random() * luck.length));

    if (lucknumber < 14){
        document.getElementById("demo").innerHTML = "SR";
    } 
    else {
        document.getElementById("demo").innerHTML = "SSR";
        }
}
</script>

Yeah, it's a basic code but I am a beginner, so any answers will be really appreciated! Thank you!

Bjamse
  • 333
  • 5
  • 17

1 Answers1

1

You can use a timeout for your function

...
var inner = "SSR";
if (lucknumber < 14){
  inner = "SR";
}

setTimeout(function(){ 
  document.getElementById("demo").innerHTML = inner;
}, 2000);

...
  • thank you! but i'm having trouble embedding this code onto my piece of code, I don't know where to put where. If it's not too much of a problem, could you point me in the right direction? :D thanks! – moon river. Jan 30 '18 at 22:16
  • In your if statement, before `document`. Instead of `alert()` replace it with `document.getElementById()` `3000` is **milliseconds**. – Vladimir Jovanović Jan 30 '18 at 22:21