473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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>

Mar 4 '06 #1
2 1745
libsfan01 wrote:
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
Mar 4 '06 #2
JRS: In article <11**********************@z34g2000cwc.googlegroups .com>
, dated Sat, 4 Mar 2006 04:52:36 remote, seen in
news:comp.lang.javascript, libsfan01 <ma********@msn.com> posted :
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob | last post by:
Trying to convert timestamp data to date data (without the time in hours/mins/secs), does only seem to work with TO_DATE(mydate; 'yyyymmdd'). But I do not want a string, but a real date like...
3
by: Bonnett | last post by:
I have been creating a generic countdown timer (source code below), counting the seconds, minutes, hours and days till an event, but I have having trouble with it finding out how many hours are...
6
by: lauren quantrell | last post by:
I am using a timer to display the time it takes a user to complete a file transfer using the code below. The code below will display the total elapsed time, but is there a way to update the time...
5
by: caulker | last post by:
I have a simple Windows Service with a timer (system.timer). The service seems to run just fine when I have the Timer = 1 minute, but when I set the Timer = 15 minutes it only fires once ... then...
7
by: Noozer | last post by:
I have a timer on a form. It isn't firing at all. I know that the timer is enabled, and that the interval is low (4000, which should be 4 seconds). To ensure the timer wasn't being inadvertantly...
6
by: Ben | last post by:
I am trying to write codes in VB.net to display 3 images in series on a Webform with each image being displayed in the same box for 5 seconds. I am trying to use Timer and image button, don't...
13
by: patelxxx | last post by:
I'm setting up a quiz where users have 120 secs to finish a quiz, what timer function do I use and how? After 120 secs I want to display a message, "Sorry timed out". Thank you patelxxx
1
by: nadeemhannan | last post by:
I need the code to write a timer, which will call webservice every 5 mins. and the web service function call will take two parameters: USERID and COMPUTER NAME/IP and will return true/false. If it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.