JS syntax help: How to set timeout inside an if statement

Hello everyone, I have a very simple question I couldn’t find the exact answer to on google.

list.addEventListener('click', e => {

if(e.target.classList.contains('delete'))
e.target.parentElement.remove();
});

How do I add a setTimeout property to this? Whenever I do I end up breaking the function completely. I need a more experienced developer’s advice/
Thank you in advance.

Hello!

You’re trying to add a delay to the removal of the parent element, right? If so, then this should work:

list.addEventListener('click', e => {
  if (e.target.classList.contains('delete')) {
    const handle = setTimeout(() => {
      e.target.parentElement.remove();
      clearTimeout(handle);
    }, 1000);
  }
});

This would remove the parent element after 1 second (1000 milliseconds).

Hope it helps :slight_smile:.

Yes this is exactly what I wanted. I see what I was doing wrong now, thank you.