Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140705

How do I synchronously run code after a setTimeout() is run?

$
0
0

I need the

event.target.innerHTML = FULL_HEART
event.target.setAttribute('class', 'like-glyph activated-heart')

after the displayError() setTimeout interval is completed in the scenario that the mimicServerCall() catches an error.

document.addEventListener('DOMContentLoaded', () => {
  const likeButtons = document.querySelectorAll('.like-glyph');
  const modal = document.getElementById('modal');

  function displayError() {
    modal.removeAttribute('class');
    setTimeout(function(){ modal.setAttribute('class','hidden') }, 3000);
  };

  function likifyMe(button) {
    button.addEventListener('click', (event) => {
      if (event.target.innerHTML === EMPTY_HEART) {
        mimicServerCall().then(() => {}).catch(() => {displayError()});
        event.target.innerHTML = FULL_HEART
        event.target.setAttribute('class', 'like-glyph activated-heart')
      } else {
        event.target.innerHTML = EMPTY_HEART
        event.target.setAttribute('class', 'like-glyph')
      }
    });
  };

  likeButtons.forEach(button => {
    likifyMe(button)
  });

});

Is there a way to achieve this?


Viewing all articles
Browse latest Browse all 140705

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>