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

Javascript InnerHTML display for two seconds

Okay so I’ve looked over previous similar questions and I’m not quite getting the answer. I have the below function that will display nicely but I want the display to end after two seconds. The second function was a a little experiment but delays the response for two seconds rather than displaying for two seconds. Any help would be great.

var j = 0;
function buttonClick() {
    text = ++j + " added to cart"
    document.getElementById('rope111').innerHTML = text;
}



var j = 0;
function buttonClick() {
    text = ++j + " added to cart"
    setTimeout(function(){document.getElementById('rope111').innerHTML = text}, 2000);
}

>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

This should work:

var j = 0;
function buttonClick() {
    text = ++j + " added to cart";
    // First, we display the text with the statement below.
    document.getElementById('rope111').innerHTML = text;
    setTimeout(function() {
        // The we remove the text after 2 seconds.
        document.getElementById('rope111').innerHTML = "";
    }, 2000);
}

You can also replace the statement in setTimeout function with:

document.getElementById('rope111').style.display = "none";
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