G
graham.reeds
I am updating a website that uses a countdown script embedded on the
page. When the page is served the var's are set to how long the
countdown has left in minutes and seconds, but the rest of the script
is left untouched.
However I want to take the script out of the page and have it as a
seperate file that can be cached, reducing serving costs - the page
gets hit a couple of thousand times per day, sometimes as high a 5K, so
any savings are multiplied.
The original script is:
----------
var min=40;
var sec=40;
function countdown()
{
if (sec==0 && min>0)
{ sec=59; min--; }
else
{ sec--; }
msg = '<b>Next Tick: ' + min + ':' + ((sec < 10) ? '0' : '') + sec +
'</b>';
if (document.all)
document.all.tel.innerHTML = msg;
else if (document.getElementById('tel'))
document.getElementById('tel').innerHTML = msg;
if (sec==0 && min==0)
{ min=60; sec=00; }
if (sec>0 || min>0)
setTimeout('countdown()',1000);
}
window.onload=countdown;
----------
Note: the var are set to 40 for testing purposes. In the actual script
they are set to the time that is left on the server.
So I thought the easiest way to do this would make the function accept
two vars, and then pass the parameters to the function from within the
html.
However the closest I can get is this:
----------
function countdown(m, s)
{
sec = s;
min = m;
if (sec==0 && min>0)
{ sec=59; min--; }
else
{ sec--; }
msg = '<b>Next Tick: ' + min + ':' + ((sec < 10) ? '0' : '') + sec +
'</b>';
if (document.all)
document.all.tel.innerHTML = msg;
else if (document.getElementById('tel'))
document.getElementById('tel').innerHTML = msg;
if (sec==0 && min==0)
{ min=60; sec=00; }
if (sec>0 || min>0)
setTimeout("countdown(min,sec)",1000);
}
window.onload=countdown(40,40);
----------
And this code only works in FF - it stops working in IE6. As soon as I
move the 'window.onload' line out of the .js file and into the HTML
code it stops working in FF too. I thought the code would be the
following, contained in <head>:
----------
<script src="scripts/countdown.js" type="text/javascript">
<!--
window.onload=countdown(40,40);
//-->
</script>
page. When the page is served the var's are set to how long the
countdown has left in minutes and seconds, but the rest of the script
is left untouched.
However I want to take the script out of the page and have it as a
seperate file that can be cached, reducing serving costs - the page
gets hit a couple of thousand times per day, sometimes as high a 5K, so
any savings are multiplied.
The original script is:
----------
var min=40;
var sec=40;
function countdown()
{
if (sec==0 && min>0)
{ sec=59; min--; }
else
{ sec--; }
msg = '<b>Next Tick: ' + min + ':' + ((sec < 10) ? '0' : '') + sec +
'</b>';
if (document.all)
document.all.tel.innerHTML = msg;
else if (document.getElementById('tel'))
document.getElementById('tel').innerHTML = msg;
if (sec==0 && min==0)
{ min=60; sec=00; }
if (sec>0 || min>0)
setTimeout('countdown()',1000);
}
window.onload=countdown;
----------
Note: the var are set to 40 for testing purposes. In the actual script
they are set to the time that is left on the server.
So I thought the easiest way to do this would make the function accept
two vars, and then pass the parameters to the function from within the
html.
However the closest I can get is this:
----------
function countdown(m, s)
{
sec = s;
min = m;
if (sec==0 && min>0)
{ sec=59; min--; }
else
{ sec--; }
msg = '<b>Next Tick: ' + min + ':' + ((sec < 10) ? '0' : '') + sec +
'</b>';
if (document.all)
document.all.tel.innerHTML = msg;
else if (document.getElementById('tel'))
document.getElementById('tel').innerHTML = msg;
if (sec==0 && min==0)
{ min=60; sec=00; }
if (sec>0 || min>0)
setTimeout("countdown(min,sec)",1000);
}
window.onload=countdown(40,40);
----------
And this code only works in FF - it stops working in IE6. As soon as I
move the 'window.onload' line out of the .js file and into the HTML
code it stops working in FF too. I thought the code would be the
following, contained in <head>:
----------
<script src="scripts/countdown.js" type="text/javascript">
<!--
window.onload=countdown(40,40);
//-->
</script>