...

View Full Version : .js math problem and counting down



GT3_fiend
07-11-2002, 06:22 PM
heres a script i pasted together from other sources; it works ( yes isnt that amazing) but what i need is a way to convert the seconds to minutes and seconds to be displayed to the user. i know its done with math but i cant really figure it out in .js

can someone PLEASE help me to do this?
besides you get the used of a wonderful timed redirect script thats not embedded in a form and is cross browser tested.


heres the script

<script language="JavaScript">

//Redirect without form-tag page script

//change below target URL to desired place
var targetURL="http://www.cnn.com"
//configure redirect time (in seconds)
var countDownInterval=1800;
//configure width of displayed text, in px (applicable only in NS4.x)
var c_redirectwidth=200

</script>


<ilayer id="c_redirect" width=&{c_redirectwidth}; ><layer id="c_redirect2" width=&{c_redirectwidth}; left=0 top=0 z-index:3></layer></ilayer>

<script>

var countDownTime=countDownInterval+1;
function countDown(){
countDownTime--;
if (countDownTime <=0){
countDownTime=countDownInterval;
clearTimeout(counter)
window.location=targetURL
return
}
if (document.all) //if IE 4+
document.all.countDownText.innerText = countDownTime+" ";
else if (document.getElementById) //else if NS6+
document.getElementById("countDownText").innerHTML=countDownTime+" "
else if (document.layers){ //TEXT FOR NS4.x
document.c_redirect.document.c_redirect2.document.write('Next redirect in <b id="countDownText">'+countDownTime+' </b> seconds')
document.c_redirect.document.c_redirect2.document.close()
}
counter=setTimeout("countDown()", 1000);
}

function startit(){
if (document.all||document.getElementById) //TEXT FOR IE
document.write('Next redirect in <b id="countDownText">'+countDownTime+' </b> seconds')
countDown()
}

if (document.all||document.getElementById)
startit()
else
window.onload=startit

</script>
<!--end countdown script -->

RadarBob
07-11-2002, 08:32 PM
Here ya go. This is your code with the changes highlighted.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Page title</title>
</head>
<body>

<script language="JavaScript">

//Redirect without form-tag page script

//change below target URL to desired place
var targetURL="http://www.cnn.com"

//configure redirect time (in seconds)
var countDownInterval=1800;

//configure width of displayed text, in px (applicable only in NS4.x)
var c_redirectwidth=200

</script>


<ilayer id="c_redirect" width=&{c_redirectwidth}; ><layer id="c_redirect2" width=&{c_redirectwidth}; left=0 top=0 z-index:3></layer></ilayer>

<script>

var countDownTime=countDownInterval+1;

function countDown(){
countDownTime--;
if (countDownTime <=0){
countDownTime=countDownInterval;
clearTimeout(counter)
window.location=targetURL
return
}
if (document.all) //if IE 4+
document.all.countDownText.innerText = secToMin(countDownTime)+" ";
else if (document.getElementById) //else if NS6+
document.getElementById("countDownText").innerHTML=secToMin(countDownTime)+" "
else if (document.layers){ //TEXT FOR NS4.x
document.c_redirect.document.c_redirect2.document.write('Next redirect in <b id="countDownText">'+secToMin(countDownTime)+' </b> seconds')
document.c_redirect.document.c_redirect2.document.close()
}
counter=setTimeout("countDown()", 1000);
}

function startit(){

if (document.all||document.getElementById) { //TEXT FOR IE
document.write('Next redirect in <b id="countDownText">' + secToMin(countDownTime) +' </b> seconds');
countDown();
}
}

function secToMin (theseconds) {
var theTime = new String();
var minutes = Math.floor (theseconds /60);
var seconds = theseconds % 60;

theTime = minutes + " Minutes " + seconds;
return theTime;
}

if (document.all||document.getElementById)
startit()
else
window.onload=startit

</script>
<!--end countdown script -->


</body>
</html>

Alex Vincent
07-11-2002, 08:32 PM
Hm. Well, the code itself is probably a bit bloated, in my opinion. The best cross-browser solution would use a form text input as the place to store the counter.

It looks like the countDownTime variable is where the actual number of seconds is stored.

So:

var minutes = Math.floor(countDownTime / 60)
var seconds = countDownTime % 60

That should do it. The % operator is called a "mod" operator, which gives you the remainder after dividing (by 60, in this case).

EDIT: Hehe, I must be losing my touch.

GT3_fiend
07-11-2002, 09:46 PM
thanks so much ; your change really did the trick!!


(one day i wanna grow up and be just like you guys!)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum