在XUL JavaScript中10秒自动关闭提示警报框

Close Prompt alert box automatically with 10Seconds in XUL JavaScript

本文关键字:提示 JavaScript XUL 10秒      更新时间:2023-09-26

这是我在XUL中的提示警报框功能:函数promptBoxes ()

{
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                        .getService(Components.interfaces.nsIPromptService);
var check = {value: false};                  // default the checkbox to false
var flags = prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_Ok+
            prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_IS_STRING;
var button = prompts.confirmEx(null, "Title of this Dialog", "What do you want to do?",
                               flags, "", "Cancel", "", null, check);
// 0, 1, or 2.
}

以上功能是我从本网站获取的:https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIPromptService alertCheck_example

如何在10秒内自动关闭此提示框(显示消息:此提示框将在10秒内关闭,并在框内显示计时器)?

如何将此框放置在系统的角落显示?

我在Mozilla提示服务中找不到任何定时器细节

我不认为这是可能的内置提示,但你可以很容易地做到这一点,自定义提示窗口

1)创建XUL对话框alert_prompt。

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
<dialog id="alertprompt" title="Alert"
   xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
   buttons="accept,cancel"
   buttonlabelcancel="Cancel"
   buttonlabelaccept="Save"
   height="140"
   width="250"
   ondialogaccept="return alert_prompt.doOK();"
   ondialogcancel="return alert_prompt.doCancel();">
   <script type="application/javascript" src="chrome://hello/content/alert_prompt.js"/>
    <dialogheader title="Timer Alert Prompt"/>
    <label id="result" value="This prompt will close in 10 seconds." align="center"/>
</dialog>

2)为XUL窗口创建一个Javascript文件alert_prompt.js

var alert_prompt = {
init : function()
{
    alert_prompt.timedCount(0);
},
timedCount : function(c)
{
    //update the prompt message
    document.getElementById('result').value="This prompt will close in "+ (10 - c) + " seconds.";
    //if 10 seconds are over close the window
    if(c == 10)
    {
        window.close();
    }
    //update the counter
    c=c+1;
    //use the timer
    t=setTimeout(
        function()
        {
            alert_prompt.timedCount(c);
        },1000)
},
doOK : function()
{
    //code that you want to run when save button is pressed 
    return true;
},
doCancel : function()
{
    //code that you want to run when cancel button is pressed 
    return true;
},
};
window.addEventListener("load", alert_prompt.init, false);

3)不像前面那样显示警告提示,使用下面的语句:

openDialog("chrome://hello/content/alert_prompt.xul","alert_prompt","modal");

如果你想从警告框返回一个值,比如哪个按钮被按下了,你可以用同样的方式来做,这里

我不确定模态窗口的定位,所以你可能想在一个单独的问题中问。