Jump to content

countdown with js


smartalco

Recommended Posts

I'm making a senior countdown script, and can calculate the time, and I believe I have it set up so that it decrements (is that a word?) the variables, however, I don't know how to make it change on the page, in other situations using js to change something you change aspects of an element (example: changing the source of an image tag), but I don't know what I would change to make this updatecode so far

<?phpfunction date_diff($d1, $d2){    $d1 = (is_string($d1) ? strtotime($d1) : $d1);    $d2 = (is_string($d2) ? strtotime($d2) : $d2);    $diff_secs = abs($d1 - $d2);    $base_year = min(date("Y", $d1), date("Y", $d2));    $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);    return array(        "years" => date("Y", $diff) - $base_year,        "months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,        "months" => date("n", $diff) - 1,        "days_total" => floor($diff_secs / (3600 * 24)),        "days" => date("j", $diff) - 1,        "hours_total" => floor($diff_secs / 3600),        "hours" => date("G", $diff),        "minutes_total" => floor($diff_secs / 60),        "minutes" => (int) date("i", $diff),        "seconds_total" => $diff_secs,        "seconds" => (int) date("s", $diff)    );}$a = date_diff("now", "2008-5-20 3:00pm");echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Senior Countdown</title></head><body><script type="text/javascript">var months="' . $a["months"] . '";var days="' . $a["days"] . '";var hours="' . $a["hours"] . '";var minutes="' . $a["minutes"] . '";var seconds="' . $a["seconds"] . '";';//echo $a["months"] . " Months, " . $a["days"] . " Days, " . $a["hours"] . " Hours, " . $a["minutes"] . " Minutes, " . $a["seconds"] . " Seconds until graduation.";?>function countdown() {		seconds = seconds - 1;        if(seconds < 0) {    	seconds = 59;        minutes = minutes - 1;    }	if(minutes < 0) {    	minutes = 59;        hours = hours - 1;    }	if(hours < 0) {    	hours = 23;        days = days - 1;    }	if(days < 0) {    	days = 30;        months = months - 1;    }		setTimeout("countdown();", 1000);	}    document.write(months);document.write(" months, ");document.write(days);document.write(" days, ");document.write(hours);document.write(" hours, ");document.write(minutes);document.write(" minutes, and ");document.write(seconds);document.write(" seconds until graduation.");</script><script type="text/javascript">countdown();</script></body></html>

Link to comment
Share on other sites

I believe your problem is the use of document.write to render the contents. Once the page has finished loading (window.onload), the document object is implicitly closed automatically by the browser. The next time document.write is accessed, the document object is opened - implicitly - and any content that you write to the page overwrites the previous content - javascript and all.Try using a div with an id and set the innerHTML rather than use document.write:

<div id="output"></div>

var output = months;output += " months, ";output += days;output += " days, ";output += hours;output += " hours, ";output += minutes;output += " minutes, and ";output += seconds;output += " seconds until graduation.";document.getElementById("output").innerHTML = output;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...