Link to home
Start Free TrialLog in
Avatar of inverted_2000
inverted_2000Flag for United States of America

asked on

Java Timer - Adding a 5 Minute Warning Function

Hey everyone...

I'm not to great at JavaScript...but I'm trying.  I got a script to create a counter and now I need to add the functionality of 5 minute warning window that will appear when the counter gets down to 5 minutes.  I've got this script set to count down from 15 minutes (980 seconds) but when I tried to add to the script the counter stopped working.  Please correct my script.  I've been staring at it so long that I don't know what I'm looking at anymore (o:  I added the stopFive function and the 1st "else" statement.

Thanks a ton

<SCRIPT LANGUAGE="JavaScript">
<!--
var running = false
var endTime = null
var timerID = null
function startTimer() {
    running = true
    now = new Date()
    now = now.getTime()
    // change last multiple for the number of minutes
    endTime = now + (1000 * 980 * 1)
    showCountDown()
}
function showCountDown() {
    var now = new Date()
    now = now.getTime()
    if (endTime - now <= 0) {
        stopTimer()
alert("Your Time is up.  Please Advise Your Testing Manager That You're Finished.")
    }
      else {
            var now = new Date()
            now = now.gettime()
            if (endTime - now = 300){
                  stopFive()
                  alert("You Have 5 Minutes Remaining.")
                  }
            }
      else {
        var delta = new Date(endTime - now)
        var theMin = delta.getMinutes()
        var theSec = delta.getSeconds()
        var theTime = theMin
        theTime += ((theSec < 10) ? ":0" : ":") + theSec
        document.forms[0].timerDisplay.value = theTime
        if (running) {
            timerID = setTimeout("showCountDown()",1000)
        }
    }
}
function stopFive() {
    clearTimeout(timerID)
    running = false
    document.forms[0].timerDisplay.value = "5:00";
}
function stopTimer() {
    clearTimeout(timerID)
    running = false
    document.forms[0].timerDisplay.value = "0:00"
      window.location.replace ( "/mt/secure/Questions/Finished.cfm" ) ;
}
//-->
</SCRIPT>
SOLUTION
Avatar of smaccari
smaccari

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of inverted_2000

ASKER

Is that little bit of code supposed to replace all of the code from my first post?

Your post has an error that states endTime is undefinied...

But yes...that is what I am looking to do.
Avatar of smaccari
smaccari

Just replace your showCountDown() fucntion with this one, and delete your showFive function, no more called.

endTime will not be undefined if you call your startTimer function to start the job though..
Nevermind....that was definied...I just didn't replace the correct portion of code.
Why is the second count so high?
<<<!-----else {
              if (endTime - now == 300000) {

I've added the code and the timer is counting down and at 0 is redirecting, but no warning message is present.  I've changed the time values to start counting at 15 seconds and warning at 10 seconds and redirecting at 1 second.  Please tweek it just a little more to get the 10 second prompt to appear.

Thanks a bunch again,

<SCRIPT LANGUAGE="JavaScript">
<!--
var running = false
var endTime = null
var timerID = null
function startTimer() {
    running = true
    now = new Date()
    now = now.getTime()
    // change last multiple for the number of minutes
    endTime = now + (1000 * 15 * 1)
    showCountDown()
}
function showCountDown() {
    var now = new Date()
    now = now.getTime()
    if (endTime - now <= 0) {
        stopTimer()
alert("Your Time is up.  Please Advise Your Testing Manager That You're Finished.")
    }
         else {
         if (endTime - now == 300000) {
       
           alert("You Have 5 Minutes Remaining.")
        }
        var delta = new Date(endTime - now)
        var theMin = delta.getMinutes()
        var theSec = delta.getSeconds()
        var theTime = theMin
        theTime += ((theSec < 10) ? ":0" : ":") + theSec
        document.forms[0].timerDisplay.value = theTime
        if (running) {
            timerID = setTimeout("showCountDown()",1000)
        }
    }
}
function stopTimer() {
    clearTimeout(timerID)
    running = false
    document.forms[0].timerDisplay.value = "0:00"
     window.location.replace ( "/mt/secure/Questions/Finished.cfm" ) ;
}
//-->
</SCRIPT>
Um, maybe a stupid question, but if you want to countdown from 15 minutes, shouldn't you use 900 seconds (15*60=900) ? 980 seconds is actually 16 mins and 20 seconds ...
not a stupid question you asked....I've just got 10 things going on at once....I posted a mistake....sorry (o:
I don't mean to be a bother...but what if the function showCountDown() is set to look like:

function showCountDown() {
    var now = new Date()
    now = now.getTime()
    if (endTime - now <= 0) {
        stopTimer()
alert("Your Time is up.  Please Advise Your Testing Manager That You're Finished.")
    }
         else {
         if (endTime - now == 1000 * 10 * 1) {
       
           alert("You Have 5 Minutes Remaining.")
        }
        var delta = new Date(endTime - now)
        var theMin = delta.getMinutes()
        var theSec = delta.getSeconds()
        var theTime = theMin
        theTime += ((theSec < 10) ? ":0" : ":") + theSec
        document.forms[0].timerDisplay.value = theTime
        if (running) {
            timerID = setTimeout("showCountDown()",1000)
        }
    }
}
300000 was for 5 minutes though.
endTime and now are in milliseconds, (that's what returns the getTime method).

For your last post, the alert "5 minutes remaining" will pop at 10 sec before the end.

So if you want a 15 min timer, endTime should be 1000 * 60  * 15. (your test being  if (endTime - now == 1000 * 60 * 5 ) if you want a pop 5 min before the end)

If you want a 15 sec timer with a pop 10 sec before the end, then enTime should be 1000 * 15, and your test
 if (endTime - now == 1000 * 10 ).
I'll add more points because this seems to be more complex then I 1st thought.  I've got your code and I really really appreciate it....but the warning....and I'm using the 15 second and 10 second scheme.....will not popup at 10 seconds.

<SCRIPT LANGUAGE="JavaScript">
<!--
var running = false
var endTime = null
var timerID = null
function startTimer() {
    running = true
    now = new Date()
    now = now.getTime()
    // change last multiple for the number of minutes
    endTime = now + (1000 * 15)
    showCountDown()
}
function showCountDown() {
    var now = new Date()
    now = now.getTime()
    if (endTime - now <= 0) {
        stopTimer()
alert("Your Time is up.  Please Advise Your Testing Manager That You're Finished.")
    }
         else {
         if  (endTime - now == 1000 * 10 ) {
       
           alert("You Have 5 Minutes Remaining.")
        }
        var delta = new Date(endTime - now)
        var theMin = delta.getMinutes()
        var theSec = delta.getSeconds()
        var theTime = theMin
        theTime += ((theSec < 10) ? ":0" : ":") + theSec
        document.forms[0].timerDisplay.value = theTime
        if (running) {
            timerID = setTimeout("showCountDown()",1000)
        }
    }
}
function stopTimer() {
    clearTimeout(timerID)
    running = false
    document.forms[0].timerDisplay.value = "0:00"
     window.location.replace ( "/mt/secure/Questions/Finished.cfm" ) ;
}
//-->
</SCRIPT>
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, take this duplicate line out of startTimer()

    alarmAmount = 1/6;

* dakyd, because you are doing several calculations in JavaScript each loop, you should rely on the endTime, calculated at the beginning of the program, as your ending time.  If you subtract one from numSecondsLeft after a 1000 ms delay, you'll provide more time than you really want to, because each real loop will be (calculation time + 1000 ms).  You'll note that I've reduced the setTimeout in my version to 900 ms.  But that is just how often to update the display.  It does not affect the ending time.  I noticed a skip from 0:15 down to 0:13 (because it missed the chance to display 0:14).

Peace and joy.  mvan
Thanks for everyone's help,

smaccari was close but it didn't function completely...dakyd's code did just what I wanted....and mvan01's worked but he was a few minutes too late.

Thanks everyone for your help...you guys are great (o:
You are welcome.  Peace and joy.  mvan
A note to all,

inverted_2000 was looking for a test timer which issued a warning, prior to the end of the countdown.  Dakyd's code did that, but while the alert message is on the screen, the countdown is paused - time does NOT continue to expire.  A student could presumably work on the on-screen problem and not click the [OK] button until ready to enter their answer.  At that point, the timer will resume its countdown at the point where it paused, with the full 5 minutes still remaining to be counted down.

In the solution which I presented, while the alert box is displayed, I'll admit that the countdown display is suspended (that's how alert() works), but the time continues to expire.  Upon clicking [OK], the countdown display will show the correct time remaining, from the original starting time.  Thus no extra time is given for delaying acknowledgement of the "5 minute warning."

In fact, the ONLY good thing about the alert box in this case, is that it makes a sound (depending on operating system).  I've looked around to find another way, and playing a .wav file seems to be the simplest.  If you don't need a system generated sound, then a popup window or displaying text previously hidden on the page would be a better solution - then either timer could keep on ticking.  :-)

Peace and joy.  mvan