Javascript 自动关闭警报

Javascript 自动关闭警报,javascript,jquery,alert,auto-close,Javascript,Jquery,Alert,Auto Close,有没有办法自动关闭javascriptalert() 我有个警报 alert("Error found"); 几秒钟后我想把它关上。这是可能的还是我应该使用jQuery对话 此功能不适用于警报。但是,您可以使用div function tempAlert(msg,duration) { var el = document.createElement("div"); el.setAttribute("style","position:absolute;top:40%;left:20%;bac

有没有办法自动关闭javascript
alert()

我有个警报

alert("Error found");
几秒钟后我想把它关上。这是可能的还是我应该使用jQuery对话

此功能不适用于警报。但是,您可以使用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);
}
这样使用:

tempAlert("close",1000);
alertTimeout("System Message<br>This is a test message<br>This alert will auto-close",5000)

你无论如何都不能关闭警报

但是您可以使用div来显示您的警报消息

  function Mymsg(msg,duration)
{
 var alt = document.createElement("div");
     alt.setAttribute("style","position:absolute;top:50%;left:50%;background-color:white;");
     alt.innerHTML = msg;
     setTimeout(function(){
      alt.parentNode.removeChild(alt);
     },duration);
     document.body.appendChild(alt);
}
您可以将其用作:

Mymsg('close',2000)

我更新了样式设置,因此它像一个启动屏幕一样显示在页面的中心,并将文本居中显示

可以这样称呼:

tempAlert("close",1000);
alertTimeout("System Message<br>This is a test message<br>This alert will auto-close",5000)

基本上,您可以模拟带有弹出窗口的警报框,并随时关闭它,调用以下函数:

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);
}
function myAlert(msg,title,width,height,timeout) {
    var myWindow = window.open("", "",`width=${width},height=${height},left=${(window.outerWidth/2-width/2)},top=0`); //open a new popup at the top height and middle width of the current window
    myWindow.document.write(`<center id="msg">`+msg+`</center>`); //write the message you want to display
    myWindow.document.title = title; //write the title of the alert box
    setTimeout(function(){
        myWindow.close(); //close the popup
    },timeout||3000) //in 3 secondes (3000 milliseconds)
}

从函数myAlert()

我认为这是不可能的;6年来从未见过它。
@sreekesh
你不能关闭警报框,只能隐藏它。@ankit337:如何隐藏?Thanx伙计们。我想我会使用jQuery对话:)