Autoclose alert

时光毁灭记忆、已成空白 提交于 2019-12-23 07:29:17

问题


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

I have an alert

alert("Error found");

I want to close it after a few second. Is that possible or shall I go for jQuery dialogue


回答1:


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",1000);



回答2:


You can't close alert any how .

But you can use div To show your alert MSG.

  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);
}

You can use as :

Mymsg('close',2000)



回答3:


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<br>This is a test message<br>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);
}


来源:https://stackoverflow.com/questions/19020830/autoclose-alert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!