SP1
SP1

Reputation: 1202

continue execution while alert box is on

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;
}

Upvotes: 0

Views: 3100

Answers (2)

andreapier
andreapier

Reputation: 2958

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!

Upvotes: 2

Jamie Wong
Jamie Wong

Reputation: 18350

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.

Upvotes: 7

Related Questions