S
S
Sergey Pantileev2018-03-28 23:49:34
JavaScript
Sergey Pantileev, 2018-03-28 23:49:34

How to create infinite repeat js script?

Help me please !!! There is a JS code that shows notifications and hides them. After opening and showing the last 3 notifications, the code stops. How to make code repeat indefinitely? That is, after the disappearance of 3 blocks, 1 2 3 appeared again and so on endlessly

window.addEventListener("DOMContentLoaded", function() {
    function c() {
        if (a = b.shift()) a.querySelector(".close").addEventListener("click", function(a) {
            a.preventDefault();
            d()
        }), a.classList.add("show"), e = window.setTimeout(d, 3000) //время просмотра
    }

    function d() {
        window.clearTimeout(e);
        a && a.classList.remove("show");
        window.setTimeout(c, 2000) //пауза между показами
    }
    var b = document.querySelectorAll(".parent_popup"),
        e, a, b = [].slice.call(b, 0);
    window.setTimeout(c, 5000) //пауза перед 1 запуском
});

This is the code for pop up and disappear notifications
<div class="parent_popup" >
<div class="popup">
<h1>«Всплывающ 1</h1>
    <p>реклама какого-либо продукта, подписная форма, форма регистрации или же форма обратной связи.</p>
<a class="close"title="Закрыть" >X</a>
</div>
</div>

<div class="parent_popup" >
  <div class="popup">
<h1>«Второе окно 2</h1>
    <p> подписная форма, форма регистрации или же форма обратной связи.</p>
<a class="close"title="Закрыть" >X</a>
</div>
</div>

<div class="parent_popup" >
  <div class="popup">
<h1>«Третье окно 3</h1>
    <p> подписная форма, форма регистрации или же форма обратной связи.</p>
<a class="close"title="Закрыть" >X</a>
</div>
</div>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Aleksey Solovyev, 2018-03-29
@Klimanovo

https://jsfiddle.net/a3pvkhwp/1/

const popup = document.querySelectorAll('.popup');
let counter = 0;
setInterval(function() {
  popup[counter].classList.remove('show');
  counter++;
  counter === popup.length ? counter = 0 : console.log('¯\\_(ツ)_/¯')
  popup[counter].classList.add('show');
}, 1000)

A
Andrew, 2018-03-28
@KickeRockK

I can’t imagine why this is, but hanging the last one on the close button is opening the first one, is it not an option?

S
Stalker_RED, 2018-03-29
@Stalker_RED

setInterval() , no?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question