7

I want to disappear the alert box after a certain amount of time. I have heard that this is impossible in Javascript, so is there another way of making this happen?

DiMono
  • 3,308
  • 2
  • 19
  • 40
Dhandabani
  • 81
  • 1
  • 1
  • 7
  • You need to design a custom alert box, read the answer.... It works –  Sep 06 '13 at 06:11
  • possible duplicate of [Javascript close alert box](http://stackoverflow.com/questions/463368/javascript-close-alert-box) –  Sep 06 '13 at 06:22

3 Answers3

4

Try this jsbin code - A custom alert box for you

http://jsbin.com/ibUrIxu/1/edit

or Try this on your .html file

Code

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script>
function customAlert(msg,duration)
{
 var styler = document.createElement("div");
  styler.setAttribute("style","border: solid 5px Red;width:auto;height:auto;top:50%;left:40%;background-color:#444;color:Silver");
 styler.innerHTML = "<h1>"+msg+"</h1>";
 setTimeout(function()
 {
   styler.parentNode.removeChild(styler);
 },duration);
 document.body.appendChild(styler);
}
  function caller()
  {
    customAlert("This custom alert box will be closed in 2 seconds","2000");
  }
  </script>
  </head>
<body onload="caller()">

</body>
</html>
  • Handy! I tweaked the style to my own preferences ([codepen](https://codepen.io/pen/XLmWZB)) but I'm sure it will get lots of use. :) – ashleedawg Jun 13 '19 at 14:11
1

Working Demo -- Here is a custom alert box and function what you need

function cancelDiv() {
        $("#div1").hide();
    }

    function ShowDiv() {
        $("#div1").css("backgroundColor", "white");
        $("#div1").show();
        setTimeout(function(){$("#div1").hide()}, 3000)
    }

    $(function () { 
        $("#div1").hide();
        $("#div1").draggable();            
    });
Sudarshan Tanwar
  • 3,537
  • 3
  • 24
  • 39
  • might be a good idea to change `function ShowDiv()` to `function showDiv()`, a capital first letter in a function name is usually reserved for identifying object creation. not that it matters, just a good format and code cleanup idea – samrap Sep 06 '13 at 05:42
  • you can't say it jugad @ deepak, this is hack or workaround in programming terms. – Sudarshan Tanwar Sep 06 '13 at 05:57
0

You can do this by using a custom alert message. One example of a notification framework is ToastR. You can also create your own message framework. You cannot dismiss a regular JavaScript alert box though.

TGH
  • 38,769
  • 12
  • 102
  • 135