I want to develop something simple, but that's making me frustrated.
I will try to express myself in the best way possible.
To make things simpler here, I'm just bringing as a example a randomized Photo Gallery, which, by clicking on the "Snap!" button, should show some random photo in div
named id='fig'
.
But! Together you will have a countdown timer, so you can clear this id='fig'
after 5 seconds. Getting div
released for a new back sample.
My problem is to unify the two distinct functions, to give this elegant air.
The randomization of the image + the regressive counter , and show both at the same time.
I confess that I can not, despite my efforts.
To illustrate the obstacle, I developed some examples. 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 been able to do this 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 I try to do it.
A countdown timer is still missing in id='txt'
.
I want this function below:
Regressive Counter
<!--
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>
Get in with the snippet code above.
In short, I want to put it all together into one function.