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 :
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";