How to convert display to mins:secs instead of secs in a timer?

L

libsfan01

Hi all! I have this javascript timer which counts down the number of
secs that i put in and then then redirects my page, what i want to know
is how to i modify the code so that i can input the number of seconds
and this creates a DISPLAY of the form MM:SS (minutes:seconds)? Which i
feel is more aesthetically pleasing to the intended users of the site.

Kind regards
Marc

<HEAD>
<SCRIPT LANGUAGE="JavaScript">
var start=new Date();
start=Date.parse(start)/1000;
var counts=100; //NUMBER OF SECONDS TO COUNTDOWN FROM
function CountDown(){
var now=new Date();
now=Date.parse(now)/1000;
var x=parseInt(counts-(now-start),10);
if(document.form1){document.form1.clock.value = x;}
if(x>0){
timerID=setTimeout("CountDown()", 100)
}else{
location.href="redirect.php"
}
}
window.setTimeout('CountDown()',100);
</script>
</HEAD>

<BODY>
<FORM NAME="form1">
You are being redirected in
<INPUT TYPE="text" NAME="clock" SIZE="5" VALUE="10">
seconds.
</FORM>
 
J

Janwillem Borleffs

libsfan01 said:
Hi all! I have this javascript timer which counts down the number of
secs that i put in and then then redirects my page, what i want to
know is how to i modify the code so that i can input the number of
seconds and this creates a DISPLAY of the form MM:SS
(minutes:seconds)? Which i feel is more aesthetically pleasing to the
intended users of the site.

<script type="text/javascript">

var s = 15;
var href = 'http://localhost/';
var interval = null;

function display() {
var min = parseInt(s/60);
var sec = s - (60 * min);
document.getElementById('display').innerHTML =
min + ' minutes, ' + sec + ' seconds';
}

function redirect() {
s--;
display();
if (!s) {
clearInterval(interval);
location = href;
}
}

window.onload = function () {
display();
interval = window.setInterval('redirect()', 1000);
}

</script>

....
You are redirected in:
<span id="display">-- minutes, -- seconds</span>


JW
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Sat, 4 Mar 2006 04:52:36 remote, seen in
news:comp.lang.javascript said:
Hi all! I have this javascript timer which counts down the number of
secs that i put in and then then redirects my page, what i want to know
is how to i modify the code so that i can input the number of seconds
and this creates a DISPLAY of the form MM:SS (minutes:seconds)? Which i
feel is more aesthetically pleasing to the intended users of the site.
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
Deprecated.

var start=new Date();
start=Date.parse(start)/1000;

That first gets the date as an Object, then a String, them parses it to
its fields, then converts to Number.

var start = new Date()/1000 // or new Date().valueOf()/1000

is simpler, though it does preserve the fractional part, which
Math.floor() or |0 will remove (|0 until 2038-01-19 03:14:08 UT IIRC).

var counts=100; //NUMBER OF SECONDS TO COUNTDOWN FROM
function CountDown(){
var now=new Date();
now=Date.parse(now)/1000;
Ditto.

var x=parseInt(counts-(now-start),10);

That subtracts Numbers (which are integer value here), converts to
String, and converts back again. Omit use of parseInt.
if(document.form1){document.form1.clock.value = x;}

And what if not?
if(x>0){
timerID=setTimeout("CountDown()", 100)
}else{
location.href="redirect.php"

if (x<=0) location.href="redirect.php"
should suffice.


Replace document.form1.clock.value = x
with s = x%60 ; m = (x-s)/60
document.form1.clock.value = m + (s>9?":":":0") + s
to get M:SS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top