title: Swiper observer random delay
body: <p>I'm building a thumbnail gallery with a slider feature using <a href="https://swiperjs.com/" rel="nofollow noreferrer">Swiper</a> (v6.5.1). By default, the slider is hidden, and when the user clicks one of the images, the slider must be displayed showing the clicked image. Once the slider is open, the user can click a Close button to hide it and return to the thumbnail gallery.</p>
  <p>This is the code I'm using:</p>
  <p>JS:</p>
  <pre><code>var swiper;
  
  swiper = new Swiper( '.gallery-slider .swiper-container', {
      loop: true,
      observer: true,
      observeParents: true,
      autoplay: {
          delay: 3000,
          disableOnInteraction: false,
      },
      navigation: {
          nextEl: '.swiper-next',
          prevEl: '.swiper-prev',
      },
  });
  
  document.querySelectorAll( '.gallery-thumbnails a[data-slide]' ).forEach( item =&gt; {
      item.addEventListener( 'click', function( e ) {
          e.preventDefault();
          window.scrollTo( 0, 0 );
          document.querySelector( '.gallery-thumbnails' ).style.display = 'none';
          document.querySelector( '.gallery-slider .swiper-container' ).style.display = 'block';
          var slideno = $( this ).data( 'slide' );
          swiper.slideTo( slideno + 1, false, false );
      });
  });
  
  document.querySelector( '.gallery-slider .close' ).addEventListener( 'click', function( e ) {
      e.preventDefault();
      document.querySelector( '.gallery-slider .swiper-container' ).style.display = 'none';
      document.querySelector( '.gallery-thumbnails' ).style.display = 'flex';
  });
  </code></pre>
  <p>CSS:</p>
  <pre><code>.gallery-slider .swiper-container {
      display: none;
  }
  </code></pre>
  <p>HTML:</p>
  <pre><code>&lt;div class=&quot;gallery-thumbnails&quot;&gt;
      &lt;div class=&quot;gallery-image&quot;&gt;&lt;a href=&quot;&quot; data-slide=&quot;0&quot;&gt;&lt;img src=&quot;thumb0.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
      &lt;div class=&quot;gallery-image&quot;&gt;&lt;a href=&quot;&quot; data-slide=&quot;1&quot;&gt;&lt;img src=&quot;thumb1.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
      &lt;div class=&quot;gallery-image&quot;&gt;&lt;a href=&quot;&quot; data-slide=&quot;2&quot;&gt;&lt;img src=&quot;thumb2.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
      &lt;div class=&quot;gallery-image&quot;&gt;&lt;a href=&quot;&quot; data-slide=&quot;3&quot;&gt;&lt;img src=&quot;thumb3.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
      ....
  &lt;/div&gt;
  
  &lt;div class=&quot;gallery-slider&quot;&gt;
      &lt;div class=&quot;swiper-container&quot;&gt;
          &lt;div class=&quot;swiper-prev&quot;&gt;previous&lt;/div&gt;
          &lt;div class=&quot;swiper-next&quot;&gt;next&lt;/div&gt;
          &lt;div class=&quot;close&quot;&gt;close&lt;/div&gt;
          &lt;div class=&quot;swiper-wrapper&quot;&gt;
              &lt;div class=&quot;swiper-slide&quot;&gt;&lt;img src=&quot;slide0.jpg&quot; /&gt;&lt;/div&gt;
              &lt;div class=&quot;swiper-slide&quot;&gt;&lt;img src=&quot;slide1.jpg&quot; /&gt;&lt;/div&gt;
              &lt;div class=&quot;swiper-slide&quot;&gt;&lt;img src=&quot;slide2.jpg&quot; /&gt;&lt;/div&gt;
              &lt;div class=&quot;swiper-slide&quot;&gt;&lt;img src=&quot;slide3.jpg&quot; /&gt;&lt;/div&gt;
              ....
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  <p>As you can see, the code uses Swiper's observer feature, which reinitializes Swiper each time there's a change in its style (like hide/show) or in its child elements (like adding/removing slides).</p>
  <p>Everything works as expected the first time I click an image in the thumbnail gallery. But, if I hide the slider using the Close button and then click in a new image to open the slider again, the slider appears but is not responsive until some seconds have passed. The strange thing is that this delay is not always the same. Sometimes is almost instantaneous and sometimes more than x seconds have already passed when the slider begins to respond to clicks in the next and previous buttons. One thing I noticed is that if I resize the window, the slider becomes responsive instantly.</p>
  <p>I tried this code in the latest Chrome and Firefox with the same results.</p>
  <p>Is this the expected behavior? Is the <code>MutationObserver</code> used by the observer feature not always listening to changes in the DOM? Am I initializing Swiper the wrong way?</p>
  <p>This is the code for the Swiper observer:</p>
  <p><a href="https://github.com/nolimits4web/swiper/blob/master/src/modules/observer/observer.js" rel="nofollow noreferrer">https://github.com/nolimits4web/swiper/blob/master/src/modules/observer/observer.js</a></p>