I have a code in which each time I click on a button it shows me a random word from the list using a code js.
Now I want the word not to be shown instantly, but it takes a few seconds to show the word and while those seconds pass it shows a gif load image . When those seconds are over, the image changes and one is shown, for example, HAS FINISHED!.
How do I schedule that delay in showing the information?
HTML
<!-- Gif mientras carga -->
<div id="cocinando">
<img src="images/cocinando.gif" alt="cocinando" title="Cocinando"/>
<p>cocinando</p>
</div>
<!-- Muestra la palabra aleatoria -->
<div class="word">
<span id="wordOutput"></span>
</div>
<button id="randomWordGen">Generate</button>
JS
document.addEventListener('DOMContentLoaded', function () {
var clickTimes = 0;
var btnRandomWord = document.getElementById('randomWordGen');
var wordOutput = document.getElementById('wordOutput');
btnRandomWord.addEventListener('click', function () {
if (clickTimes < 3) {
var request = new XMLHttpRequest();
// método HTTP y URL
request.open('GET', 'randomwordgen.php');
request.onload = function () {
// estado 4 = petición completada y respuesta recibida
if (request.readyState === 4) {
// código HTTP 200 = petición exitosa
if (request.status === 200) {
wordOutput.textContent = request.responseText;
clickTimes++;
}
}
};
request.send(); // se envía la petición
} else {
wordOutput.textContent = 'PALABRA FINAL';
}
});
});