Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you stop displaying a div element (id) after a certain amount of time?

I have been coding a website, but I want a message to disappear/not display after a few seconds. I tried different code snippets but I cannot seem to plug them in properly with my code. Is there any way I could make it stop displaying after a certain amount of time?

var done = "Done!";
document.getElementById("done").innerHTML = done;

var startTime = new Date().getTime();
var interval = setInterval(function() {
  if (new Date().getTime() - startTime > 2000) {
    clearInterval(interval);
    return;
  }
  done.getElementById.remove("done");
}, 2000);
<h2 style="color:green;" id="greendisp">
  <span id="done"></span>
</h2>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You should use setTimeout

const done = "Done!";
document.getElementById("done").innerHTML = done;

setTimeout(() => {
  document.getElementById("done").remove()
}, 2000)
<h2 style="color:green;" id="greendisp"><span id="done"></span></h2>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading