2

I have the following function that utilises the setTimeout() function

someElement.addEventListener('click', function () {
    someElement.classList.remove('active');
    someElement.classList.remove('active--show');
    setTimeout(function () {
        someElement.classList.remove('some--class')
    }, 3000);
    someElement.classList.remove('another--class');
    someElement.classList.remove('and--another--class');
    someElement.classList.remove('and--another--class--again');
});

Does the current function carry on running the following commands (code snippet below) when reaching the setTimeout() function and once 3 seconds have passed run the script inside the setTimeout() function?

someElement.classList.remove('another--class');
someElement.classList.remove('and--another--class');
someElement.classList.remove('and--another--class--again');

Or does it get to the setTimeout() function, wait 3 seconds, then run the script inside it then carry on with the rest of the original function?

Appreciate any advice on this.

Thank you

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
Liam
  • 111
  • 2
  • 6
  • 13
  • 1
    setTimeout is non-blocking. The next statement is executed immediately Why not use the console to test your theory – mplungjan Aug 17 '18 at 08:33
  • The first of the scenarios you described. – C14L Aug 17 '18 at 08:35
  • 2
    @mplungjan thanks for the link to the other question that helps me understand better. I am fairly new to javascript, using the console would have been a good choice my bad there! Thanks again – Liam Aug 17 '18 at 08:48
  • 3
    I don't think either of those "duplicate" questions answers this question - at least not directly. – sdfsdf Nov 07 '18 at 06:44
  • The last 3lines will be executed and will not be blocked by setTimeout. – Deepak Kumar May 12 '21 at 09:22

0 Answers0