Autoclose alert

后端 未结 4 2155
醉酒成梦
醉酒成梦 2020-12-04 02:05

Is there any way to close a javascript alert() automatically?

I have an alert

alert(\"Error found\");

I want to close

4条回答
  •  粉色の甜心
    2020-12-04 02:17

    I updated the style settings, so it shows like a splash screen in the center of your page, and centers the text inside it.

    Call it like this:

    alertTimeout("System Message
    This is a test message
    This alert will auto-close",5000)

    The function:

    function alertTimeout(mymsg,mymsecs)
    {
     var myelement = document.createElement("div");
    myelement.setAttribute("style","background-color: grey;color:black; width: 450px;height: 200px;position: absolute;top:0;bottom:0;left:0;right:0;margin:auto;border: 4px solid black;font-family:arial;font-size:25px;font-weight:bold;display: flex; align-items: center; justify-content: center; text-align: center;");
     myelement.innerHTML = mymsg;
     setTimeout(function(){
      myelement.parentNode.removeChild(myelement);
     },mymsecs);
     document.body.appendChild(myelement);
    }
    

提交回复
热议问题