Javascript countdown without refreshing.

F

fb

Hello everyone. I have a javascript timer counting down inside a web
page (technically a .php page). We have the page load items from a
database and refresh, but this resets our counter. So, I was wondering
if there is a way to keep the timer counting down, even though we
refresh the page (or move to a different page...we are still up in the
air on which is easier/better). I'm not usually a web developer kinda
person, I usually stick to writing general applications in C, C++ or
Java, so let me know if you need more info. The timer code follows:


<script type="text/javascript">
function display_c(start){
window.start = parseFloat(start);
var end = 0 // change this to stop the counter at a higher value
var refresh=1000; // Refresh rate in milli seconds
if(window.start >= end ){
mytime=setTimeout('display_ct()',refresh)
}
else {
alert("Time Over ");
}
}

function display_ct() {
// Calculate the number of days left
var days=Math.floor(window.start / 86400);
// After deducting the days calculate the number of hours left
var hours = Math.floor((window.start - (days * 86400 ))/3600)
// After days and hours , how many minutes are left
var minutes = Math.floor((window.start - (days * 86400 ) - (hours
*3600 ))/60)
// Finally how many seconds left after removing days, hours and minutes.
var secs = Math.floor((window.start - (days * 86400 ) - (hours *3600
) - (minutes*60)))

var x = " " + "Time left : " + minutes + " Minutes and " + secs +
" Seconds " + " ";


document.getElementById('ct').innerHTML = x;
window.start= window.start- 1;

tt=display_c(window.start);
}


</script>


<body onload=display_c(1800);>
<span id='ct' style= "background-color: black"></span>

</body>

fb
 
G

GArlington

Hello everyone. I have a javascript timer counting down inside a web
page (technically a .php page). We have the page load items from a
database and refresh, but this resets our counter. So, I was wondering
if there is a way to keep the timer counting down, even though we
refresh the page (or move to a different page...we are still up in the
air on which is easier/better). I'm not usually a web developer kinda
person, I usually stick to writing general applications in C, C++ or
Java, so let me know if you need more info. The timer code follows:

<script type="text/javascript">
function display_c(start){
window.start = parseFloat(start);
var end = 0 // change this to stop the counter at a higher value
var refresh=1000; // Refresh rate in milli seconds
if(window.start >= end ){
mytime=setTimeout('display_ct()',refresh)
}
else {
alert("Time Over ");
}

}

function display_ct() {
// Calculate the number of days left
var days=Math.floor(window.start / 86400);
// After deducting the days calculate the number of hours left
var hours = Math.floor((window.start - (days * 86400 ))/3600)
// After days and hours , how many minutes are left
var minutes = Math.floor((window.start - (days * 86400 ) - (hours
*3600 ))/60)
// Finally how many seconds left after removing days, hours and minutes.
var secs = Math.floor((window.start - (days * 86400 ) - (hours *3600
) - (minutes*60)))

var x = " " + "Time left : " + minutes + " Minutes and " + secs +
" Seconds " + " ";

document.getElementById('ct').innerHTML = x;
window.start= window.start- 1;

tt=display_c(window.start);

}

</script>

<body onload=display_c(1800);>
<span id='ct' style= "background-color: black"></span>

</body>

fb
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !

If you are a programmer (in C/C++ or any other language) consider
this: your refreshed page is equivalent to a fresh copy of your C++
(or whichever programming language) program, if you want this fresh
copy to continue doing what the previous copy started then you should
find a way of passing some parameters to your fresh copy from your
previous copy...
It is NOT that different in any programming language.
Most common (and easy) way of passing parameters to javascript is via
URL...
 
D

Dr J R Stockton

It seems that my original response was not properly distributed.
Here it is :

Thu said:
function display_ct() {
// Calculate the number of days left
var days=Math.floor(window.start / 86400);
// After deducting the days calculate the number of hours left
var hours = Math.floor((window.start - (days * 86400 ))/3600)
// After days and hours , how many minutes are left
var minutes = Math.floor((window.start - (days * 86400 ) - (hours
*3600 ))/60)
// Finally how many seconds left after removing days, hours and minutes.
var secs = Math.floor((window.start - (days * 86400 ) - (hours *3600
) - (minutes*60)))

var x = " " + "Time left : " + minutes + " Minutes and " + secs +
" Seconds " + " ";

Rather than determining days hours minutes and seconds in that order,
it
seems simpler to use the reverse order.
Start with D being the number of seconds, generally large.
s = D % 60 ; D = (D-s) / 60
m = D % 60 ; D = (D-m) / 60
h = D % 24 ; D = (D-h) / 24
giving (check that) D h m s as numbers.

If your pages all show countdowns to a stated time by the user's
clock,
then each new or refreshed page will continue the count without
additional effort. See my js-date2.htm.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. [email protected] IE6 IE7 FF2
Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/
index.html>.
<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.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top