Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

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>
0
inverted_2000
Asked:
inverted_2000
  • 7
  • 4
  • 3
  • +1
3 Solutions
 
smaccariCommented:
Is that what you're looking for?
The alert will show up when 5 minutes remaining, and countdown continue.
There is in this case no need of your stopFive function.

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)
        }
    }
}
0
 
inverted_2000Author Commented:
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.
0
 
smaccariCommented:
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..
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
inverted_2000Author Commented:
Nevermind....that was definied...I just didn't replace the correct portion of code.
0
 
inverted_2000Author Commented:
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>
0
 
dakydCommented:
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 ...
0
 
inverted_2000Author Commented:
not a stupid question you asked....I've just got 10 things going on at once....I posted a mistake....sorry (o:
0
 
inverted_2000Author Commented:
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)
        }
    }
}
0
 
smaccariCommented:
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 ).
0
 
inverted_2000Author Commented:
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>
0
 
dakydCommented:
How about using something like this instead?  Rather than try to use the milliseconds, just keep a counter of how many seconds you have left.  Start the timer using the link (though you can run it onload if you like), and then it'll loop through.  If you want more time, just change the initial value of numSecondsLeft to whatever you want.  Hope that helps.

<html>
<head>
<script type="text/javascript">
<!--
// change initial value for seconds
var numSecondsLeft = 15;
var running = false;
var timerID = null;

function startTimer() {
    running = true;
    showCountDown();
}
function showCountDown() {
  if (numSecondsLeft <= 0) {
    stopTimer();
    alert("Your Time is up.  Please Advise Your Testing Manager That You're Finished.");
  }
  else {
    if  (numSecondsLeft == 10) {
      alert("You Have 10 seconds remaining.");
    }
    var theMin = parseInt(numSecondsLeft / 60);
    var theSec = numSecondsLeft % 60;
    var theTime = theMin;
    theTime += ((theSec < 10) ? ":0" : ":") + theSec;
    document.forms[0].timerDisplay.value = theTime;
    if (running) {
      timerID = setTimeout("showCountDown()",1000)
      numSecondsLeft --;
    }
  }
}
function stopTimer() {
  clearTimeout(timerID);
  running = false;
  document.forms[0].timerDisplay.value = "0:00"
  window.location.replace ( "/mt/secure/Questions/Finished.cfm" );
}
//-->
</script>
</head>

<body>
<a href="#" onclick="startTimer(); return false;">Start Timer</a>
<form>
  <input type="text" name="timerDisplay">
</form>
</body>
</html>
0
 
mvan01Commented:
Or, this works with your previous code.  Peace and joy.  mvan

<html>
<head>
<title>Test Timer</title>

<script language="JavaScript">
<!--

var running = false
var endTime = null
var timerID = null

var totalMinutes = 1/4;
var alarmAmount = 1/6;
var alarm = true;
var alarmTime = 0;

function startTimer() {
    running = true
    now = new Date()
    now = now.getTime()
    // change last multiple for the number of minutes
    endTime = now + (1000 * 60 * totalMinutes);
    alarmAmount = 1/6;
    alarm = (alarmAmount>0);
    alarmTime = endTime - (1000 * 60 * alarmAmount);
    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 (alarm) {
            if (alarmTime - now <= 0) {
               alarm = false;
               alert("You Have "+alarmAmount+" 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()",900)
        }
    }
}
function stopTimer() {
    clearTimeout(timerID)
    running = false
    document.forms[0].timerDisplay.value = "0:00"
     window.location.replace ( "/mt/secure/Questions/Finished.cfm" ) ;
}

//-->
</script>

</head>
<body onLoad="startTimer();">
<form>
Time Remaining: <input type="text" name="timerDisplay" value="0:00:00" length=20>
</form>
</body>
</html>
0
 
mvan01Commented:
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
0
 
inverted_2000Author Commented:
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:
0
 
mvan01Commented:
You are welcome.  Peace and joy.  mvan
0
 
mvan01Commented:
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now