r/javahelp • u/hairpin1 • Jun 17 '19
Getting a countdown timer to display minutes:seconds
Hey all
Let me preface this by saying that I am an extreme beginner. I have never even viewed html markdown until a couple of coding sessions ago... much less used java. However i'm required to use both to display an experiment I am running via a website. It's extremely 90's looking but everything is pretty much done/bearable except this timer.
On certain pages I require a countdown timer to start on page open. at the end of the time limit a buzzer should sound, and the participant should be directed to the next page in the sequence. Good news is: I actually have got that working myself! (this is pathetic but i fell like a wizard right now xD). Got the timer from here, inserted my own locally hosted link, and added a new if clause for audio.play for a locally hosted sound.
The remaining problem is this: ideally i'd like to display the countdown in a minutes:seconds format. For example 1:35 instead of 95 seconds. Here's my current code for the timer (in case it is relevant its sitting in the nav bar of the html markdown):
<h3>Time Remaining: <span id="countdown">120</span> seconds</h3>
<!-- JavaScript part -->
<script type="text/javascript">
// Set Time limit
var seconds = 120;
// Define time-end buzzer
var buzzer = new Audio('buzzer.wav')
// Define count process
function countdown() {
seconds = seconds - 1;
if (seconds == 1) {
// Play buzzer
buzzer.play();
}
if (seconds < 0) {
// Direct to next page
window.location = "Task_A_3_Splash.html";
} else {
// Update remaining seconds
document.getElementById("countdown").innerHTML = seconds;
// Count down using javascript
window.setTimeout("countdown()", 1000);
}
}
// Run countdown function
countdown();
</script>
I've tried using 'var minutes = 2' to set my time limit, on the off chance that Java would "know" what a minute is. Of-course that didn't work - and i suspect the idea is dumb one. My best guess would be that I have to somehow define what one minute is as var minutes = seconds*60, and then somehow have the countdown juggle both variables... or... something? That may also be a dumb idea.
I'd like to be able to evidence more thing's i've tried as per the guidelines... but the truth of the matter is i've gone from nothing to a (crap) website with this code in it in a couple of afternoons and i've plain reached a point where I just don't know how to progress. Not necessarily looking for a finished solution, but if someone could even point me in the right direction (maybe commands, or similar examples...) that'd be such a big help!