7
I have a Javascript function that loops, it controls one element of the page that switches places with another, my goal is when you leave the page, this function stops and only comes back when the page opens again
Function:
function rodarLogos() {
setTimeout(function () {
$div = $("#logo-1 .hex-warper");
$baixo = $div.find('.hexagon-parceiros-emBaixo');
$central = $div.find('.hexagon-parceiros-central');
$cima = $div.find('.hexagon-parceiros-emCima');
setTimeout(function () {
$baixo.toggleClass('hexagon-parceiros-central hexagon-parceiros-emBaixo').parent().toggleClass('z-index-3 z-index-2 box-adicional box-adicional-50');
$central.toggleClass('hexagon-parceiros-emCima hexagon-parceiros-central').parent().toggleClass('z-index-2 z-index-1 box-adicional-50 box-adicional');
$cima.toggleClass('hexagon-parceiros-emBaixo hexagon-parceiros-emCima').parent().toggleClass('z-index-1 z-index-3');
}, 500);
rodarLogosPagamentos();
}, 5000);
}
function rodarLogosPagamentos() {
setTimeout(function () {
$div = $("#pagemento-1 .hex-warper");
$baixo = $div.find('.hexagon-parceiros-emBaixo');
$central = $div.find('.hexagon-parceiros-central');
$direita = $div.find('.hexagon-parceiros-direita');
$cima = $div.find('.hexagon-parceiros-emCima');
setTimeout(function () {
$baixo.toggleClass('hexagon-parceiros-central hexagon-parceiros-emBaixo').parent().toggleClass('z-index-4 z-index-3 box-adicional box-adicional-50');
$central.toggleClass('hexagon-parceiros-emCima hexagon-parceiros-central').parent().toggleClass('z-index-3 z-index-1 box-adicional-50 box-adicional');
$cima.toggleClass('hexagon-parceiros-direita hexagon-parceiros-emCima').parent().toggleClass('z-index-1 z-index-2');
$direita.toggleClass('hexagon-parceiros-direita hexagon-parceiros-emBaixo').parent().toggleClass('z-index-2 z-index-4');
}, 500);
rodarLogos();
}, 5000);
}
Example: I have the stack overflow guide active and google, when I exit the stack and go to google would have to pause and when back to the stack the loop would return
You can store the loop data in your localStorage, or in Cookies
– Vinicius Cainelli
Could you provide more information about your question? If "Leave the page" means close it, you can do as Vinicius commented and use Local Storage to follow where you left off. If "Leave Page" means to minimize or change tab you could listen to the event
visibilitychange
ofdocument
(examples). Anyway, you would need to clarify your problem better so that we can help you.– fernandosavio
exit the page I say change the tab active in the browser
– Lucas
In this case, you can use an API called Page Visibility. https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
– Vinicius Cainelli
I guess I still can’t make myself understand, the browser tab, like I have the stack overflow tab active and the google one, when I leave the stack and go to google would have to pause
– Lucas
I get it, and using this API you can do exactly what you want to do. Here is an example: https://codepen.io/MSEdgeDev/pen/eZGGbX
– Vinicius Cainelli
that’s right, put that there in an answer that I accept
– Lucas