Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I auto hide alert box after it showing it? [duplicate]

Tags:

javascript

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 :

  1. Show alert

  2. Hide/terminate alert within 2 seconds

like image 680
maherelghali Avatar asked Mar 17 '13 22:03

maherelghali


People also ask

How do I turn off alert box automatically?

Closing Alerts To close the alert message, add a . alert-dismissible class to the alert container. Then add class="close" and data-dismiss="alert" to a link or a button element (when you click on this the alert box will disappear).

How do I hide message alerts?

Step 1: Click on the message you want to block. Step 2: Tap the contact's name at the top of the message to open the taskbar. Step 3: Toggle Hide Alerts to the on position.

What is window dot alert?

window. alert() instructs the browser to display a dialog with an optional message, and to wait until the user dismisses the dialog. Under some conditions — for example, when the user switches tabs — the browser may not actually display a dialog, or may not wait for the user to dismiss the dialog.


1 Answers

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); 
like image 74
Travis J Avatar answered Oct 14 '22 17:10

Travis J