How can I auto hide alert box after it showing it? [duplicate]
All I want to do is, how can I auto hide alert box within specific seconds after showing it?
All I know is,
setTimeout(function() {
alert('close');
}, 5000);
// This will appear alert after 5 seconds
No need for this I want to disappear alert after showing it within seconds.
Needed scenario :
Show alert
Hide/terminate alert within 2 seconds
tldr; jsFiddle Demo
This functionality is not possible with an alert. However, you could use a div
function tempAlert(msg,duration)
{
var el = document.createElement("div");
el.setAttribute("style","position:absolute;top:40%;left:20%;background-color:white;");
el.innerHTML = msg;
setTimeout(function(){
el.parentNode.removeChild(el);
},duration);
document.body.appendChild(el);
}
Use this like this:
tempAlert("close",5000);
You can't close an alert box with Javascript.
You could, however, use a window instead:
var w = window.open('','','width=100,height=100')
w.document.write('Message')
w.focus()
setTimeout(function() {w.close();}, 5000)