JavaScript alert box with timer

前端 未结 7 1780
粉色の甜心
粉色の甜心 2020-12-02 17:19

I want to display the alert box but for a certain interval. Is it possible in JavaScript?

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 17:37

    If you want an alert to appear after a certain about time, you can use this code:

    setTimeout(function() { alert("my message"); }, time);
    

    If you want an alert to appear and disappear after a specified interval has passed, then you're out of luck. When an alert has fired, the browser stops processing the javascript code until the user clicks "ok". This happens again when a confirm or prompt is shown.

    If you want the appear/disappear behavior, then I would recommend using something like jQueryUI's dialog widget. Here's a quick example on how you might use it to achieve that behavior.

    var dialog = $(foo).dialog('open');
    setTimeout(function() { dialog.dialog('close'); }, time);
    

提交回复
热议问题