0
I want to develop something simple, but it’s making me frustrated.
I will try to express myself in the best possible way.
To simplify things around here, I’m only bringing as an example a randomized Photo Gallery, that by clicking the button "Press!" , should display some random photo on a div
so-called id='fig'
.
But! Together you will have a countdown, to clean this id='fig'
after 5 seconds. Getting the div
released for a new subsequent sample.
My problem is in unifying the two distinct functions, to give that elegant air.
To image randomization + the countdown, and show both at the same time.
I confess that I am not succeeding, despite my efforts.
To illustrate the obstacle, I have developed some examples codes. They are:
Randomize Photo
<!--
var slide = ["procurando_dory.jpg","big_buck_bunny.jpg","madagascar_2.jpg","monstros_sa_2.jpg"];
function clic() {
var randomize = Math.floor((Math.random() * slide.length));
document.getElementById('fig').innerHTML = '<img src="https://sites.google.com/site/mplayerplugin/repositorio/' + slide[randomize] + '" />';
}
-->
<center>
<div id="fig"> </div>
<span id="txt"> </span>
<hr size="1" color="silver">
<input type="button" onclick="clic()" value="Aperte!" id="troca" />
</center>
I have managed to do so in the form below:
Clear Photo
<!--
var slide = ["procurando_dory.jpg","big_buck_bunny.jpg","madagascar_2.jpg","monstros_sa_2.jpg"];
function clic() {
var randomize = Math.floor((Math.random() * slide.length));
document.getElementById('fig').innerHTML = '<img src="https://sites.google.com/site/mplayerplugin/repositorio/' + slide[randomize] + '" />';
window.setInterval(limpar, 5000); // Define os 5 segundos para limpar
}
function limpar() {
document.getElementById('fig').innerHTML = ''; // Esvazia a div id='fig'
}
-->
<center>
<div id="fig"> </div>
<span id="txt"> </span>
<hr size="1" color="silver">
<input type="button" onclick="clic()" value="Aperte!" id="troca" />
</center>
However, this is not in its full functionality the way, which I seek to do.
Still missing a countdown on id='txt'
.
I want this function below:
Counter Regressive
<!--
var i = 5;
var intervalo = window.setInterval(function() {
document.getElementById('txt').textContent = i;
i--;
}, 1000);
setTimeout(function() {
clearInterval(intervalo);
document.getElementById('fig').innerHTML = "";
}, 5000);
-->
<center>
<div id="fig"> </div>
<span id="txt"> </span>
<hr size="1" color="silver">
<input type="button" onclick="clic()" value="Aperte!" id="troca" />
</center>
Enter together with the "snippet" code above.
In short, I want to put it all together in one function.
user31050 could add a description to the entered code. It would be nice to understand better what you did. =)
– William Pereira
There is a code symbol button that you enter, but I’ve already edited it for you. The Window description can be placed above the snippet
– William Pereira
That may be it, too, but it was actually a suggestion.
– William Pereira
@user31050 The junction, between the random function and the timer, really turned out well! I had not thought of by a condition
if
inside the timer, to get this done. Congratulations!!!– Diego Henrique
Diego changed the comments to make it clearer to you understand, one thing that was changed in the code by another colleague was the loop team, it was 1 second instead of 5 seconds at each loop of repetition, maybe you also want to change in your project.
– user31050