Hey there, I am trying to add multiple javascript
countdown's to a webpage and I admit am still
very new to javascript. I cannot manage to
autostart more then one instance at a time.I am using a combination of PHP/JS/HTML on
the page. PHP is used to calculate the
number of seconds left and to loop through
how many instances are needed. Here is my
JS code. As you can see I don't think I can
have it auto load in the body because the
#seconds will not have been calculated yet.
Feel free to recommend and new scripts.<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 {mytime=setTimeout('done()',refresh)} } 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 /3600) // After days and hours , how many minutes are left var minutes = Math.floor((window.start - (hours *3600 ))/60) // Finally how many seconds left after removing days, hours and minutes. var secs = Math.floor((window.start - (hours *3600 ) - (minutes*60))) var x = hours + ":" + minutes + ":" + secs; var y = "Done!"; document.getElementById('ct').innerHTML = x; window.start= window.start- 1; tt=display_c(window.start); } function done() { var y = "Done!"; document.getElementById('ct').innerHTML = y; } </script> </head> <body onload=display_c(# of seconds left found by php function.)> <span id=ct></span> </body>
You need to give each countdown a number e.g. 1, and then duplicate all the functions and just add the number of the countdown onto the end of the function's name. V/r
Mafanikio