Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

timer - Close Prompt alert box automatically with 10Seconds in XUL JavaScript

This is my prompt alert box function in XUL: function 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.

}

I have taken the above function from this website: https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIPromptService#alertCheck_example

How could I close this box automatically within 10seconds(with display message: this prompt box will close in 1O seconds and display the timer in the box itself)?

How can position this box to shown at the corner of the system?

I don't find any timer details in Mozilla prompt service

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think this is possible with the build in prompt but you can easily do so with a custom prompt window.

1) Create a XUL dialog alert_prompt.xul as follows:

<?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) Create a Javascript file for this XUL window 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) Instead of showing the alert prompt as earlier use this statement:

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

If you want to return a value from the alert box such as which button was pressed you can do so in the same way as discussed HERE

I am not sure about the positioning of a modal window so you may want to ask that in a separate question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...