最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - continue execution while alert box is on - Stack Overflow

programmeradmin2浏览0评论

I have a Javascript block in which it creates a timer based on mouse movements. If you are not doing any activity the timer starts and when it reaches say 1 minute left time it displays an alert to the user and if the user does not respond it navigates to another page. The problem I am facing is that when I show an alert to the user the timer execution stops and it just waits for the user to press enter. What I need is that the timer should continue in the background whether the user clicks OK or not.

var mins, secs, TimerRunning, TimerID;
TimerRunning = false;

var activity;
document.documentElement.onmousemove = function () {
    clearInterval(activity);
     activity = Init();
   // activity = setInterval(saySomething, 5000);
}

function Init() //call the Init function when u need to start the timer
{
    mins = 2;
    secs = 0;
    StopTimer();
    StartTimer();
}

function StopTimer() {
    if (TimerRunning)
        clearTimeout(TimerID);
    TimerRunning = false;
}

function StartTimer() {
    TimerRunning = true;
    window.status = "if no activity is detected you will be logged out in " + Pad(mins) + ":" + Pad(secs);
    TimerID = self.setTimeout("StartTimer()", 1000);

    Check();

    if (mins == 0 && secs == 0)
        StopTimer();

    if (secs == 0) {
        mins--;
        secs = 60;
    }
    secs--;


}

function Check() {
    if (mins == 1 && secs == 0)
        toggle();
     alert("You have only 2 minutes remaining");
    if (mins == 0 && secs == 0) {

        window.location = "http://Test";

    }
}

function Pad(number) //pads the mins/secs with a 0 if its less than 10
{
    if (number < 10)
        number = number;
    return number;
}

I have a Javascript block in which it creates a timer based on mouse movements. If you are not doing any activity the timer starts and when it reaches say 1 minute left time it displays an alert to the user and if the user does not respond it navigates to another page. The problem I am facing is that when I show an alert to the user the timer execution stops and it just waits for the user to press enter. What I need is that the timer should continue in the background whether the user clicks OK or not.

var mins, secs, TimerRunning, TimerID;
TimerRunning = false;

var activity;
document.documentElement.onmousemove = function () {
    clearInterval(activity);
     activity = Init();
   // activity = setInterval(saySomething, 5000);
}

function Init() //call the Init function when u need to start the timer
{
    mins = 2;
    secs = 0;
    StopTimer();
    StartTimer();
}

function StopTimer() {
    if (TimerRunning)
        clearTimeout(TimerID);
    TimerRunning = false;
}

function StartTimer() {
    TimerRunning = true;
    window.status = "if no activity is detected you will be logged out in " + Pad(mins) + ":" + Pad(secs);
    TimerID = self.setTimeout("StartTimer()", 1000);

    Check();

    if (mins == 0 && secs == 0)
        StopTimer();

    if (secs == 0) {
        mins--;
        secs = 60;
    }
    secs--;


}

function Check() {
    if (mins == 1 && secs == 0)
        toggle();
     alert("You have only 2 minutes remaining");
    if (mins == 0 && secs == 0) {

        window.location = "http://Test";

    }
}

function Pad(number) //pads the mins/secs with a 0 if its less than 10
{
    if (number < 10)
        number = number;
    return number;
}
Share Improve this question edited Feb 20, 2012 at 21:09 Zeta 106k13 gold badges204 silver badges246 bronze badges asked Feb 20, 2012 at 20:43 SP1SP1 1,2123 gold badges27 silver badges53 bronze badges 1
  • I believe alert() (like confirm() and prompt()) is a blocking call and there's nothing you can do about it. The alternative is to simulate a modal dialog with html/css, in which case your timer code will keep running in the background. – nnnnnn Commented Feb 20, 2012 at 20:48
Add a ment  | 

2 Answers 2

Reset to default 7

Instead of using an alert, just use a modal dialog (basically a div that floats on top of everything else), or in the worst case a popup window.

I highly doubt this is feasible in javascript as it runs inside browser's ui thread...
But you can do what you want with jquery-ui (dialog widget) or similar javascript framework!

发布评论

评论列表(0)

  1. 暂无评论