以基于时间的方式消失警报框

Disappear the alert box with time based

本文关键字:消失 方式 于时间 时间      更新时间:2023-09-26

我想在一定时间后消失警报框。我听说这在Javascript中是不可能的,那么有没有另一种方法可以做到这一点呢?

试试这个 jsbin 代码 - 一个自定义的警报框

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

或在您的.html文件上尝试此操作

法典

<!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>

工作演示 - 这是一个自定义警报框和您需要的功能

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

您可以使用自定义警报消息来执行此操作。通知框架的一个示例是 ToastR。您还可以创建自己的消息框架。但是,您不能关闭常规的JavaScript警报框。