r/javahelp • • 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!

0 Upvotes

6 comments sorted by

View all comments

4

u/indivisible Jun 17 '19

Java is to JavaScript as ham is to hamster.

You may be in the wrong sub/r. ;)

That said though, this is a petty standard problem to encounter in any language.
One way to approach it is to do is integer division by 60 to get whole minutes and then a modulus operation to figure out the remainder.

var total = 145;
var minutes = (int) total / 60; 
var seconds = total % 60;

2

u/JacobJMountain Extreme Brewer Jun 17 '19

Thank you for still answering his problem, otherwise places like this will turn into stack overflow. Everyone was a beginner once.

1

u/indivisible Jun 17 '19

I understand what you mean but the hate for SO is way overblown imo.
Different site with very different goals.

I see reddit mostly as a chat room. Have any conversation you like with whoever you please. Nobody cares if they've heard your story before.

SO though is closer to a library. The same info duplicated again and again wastes resources. Also, encouraging/forcing prospective devs to do some searching beforehand is a massive skill requirement to being a successful dev.

I contribute to both sites and I may well have taken actions on SO that you'd disagree with and lump into some sort of "typical toxic SO behavior" category. That's just the way the site works, benefit to everyone is the goal, not helping any single user.
There are strengths and weakness to both approaches but I can't see either replacing the other in terms of their individual usefulness.