I made a slideshow of HTML divs for a personal project of mine, I saw in W3School how to do it and it works, however, there was a need to pause this slideshow with a button inside each div and then resume the slideshow from where it stopped.
I used this example to use Divs instead of Bootstrap.
I tried to implement a logic with global variables to control the slideshow, but without success.
Follow the code below:
<script>
var pausa = false;
function carousel() {
var i;
var x = document.getElementsByClassName("publica");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
slideIndex++;
if (slideIndex > x.length) {slideIndex = 1}
x[slideIndex-1].style.display = "block";
if(pausa){
return;
} else{
setTimeout(carousel, 7000);
}
}
</script>
<!-- divs que vão rodar em slideshow-->
<div class="publica">
<div class="titulo">
<p>Texto genérico</p>
</div>
<hr>
<div class="texto">
<span>
<p>Texto genérico</p>
</div>
<hr>
<div class="rodape">
<button onclick="javascript:pausa=true;">Pausar</button>
<button>Retomar</button>
</div>
</div>
<div class="publica">
<div class="titulo">
<p>Texto genérico</p>
</div>
<hr>
<div class="texto">
<span>
<p>Texto genérico</p>
</div>
<hr>
<div class="rodape">
<button onclick="javascript:pausa=true;">Pausar</button>
<button>Retomar</button>
</div>
</div>
<div class="publica">
<div class="titulo">
<p>Texto genérico</p>
</div>
<hr>
<div class="texto">
<span>
<p>Texto genérico</p>
</div>
<hr>
<div class="rodape">
<button onclick="javascript:pausa=true;">Pausar</button>
<button>Retomar</button>
</div>
</div>
<!-- script para iniciar a função de slideshow -->
<script>
var slideIndex = 0;
carousel();
</script>
Thanks to anyone who can help.