Javascript Count Up Timer

Count up using Javscript from 0 seconds, including minutes, hours, and days.

by Jon Fuller

HTML

<!-- add to text via html -->
<span id="count-seconds-up"><label id="days">00</label>:<label id="hours">00</label>:<label id="minutes">00</label>:<label id="seconds">00</label></span>


<script>
	var daysLabel = document.getElementById("days");
	var hoursLabel = document.getElementById("hours");
	var minutesLabel = document.getElementById("minutes");
	var secondsLabel = document.getElementById("seconds");
	var totalSeconds = 0;
	setInterval(setTime, 1000);

	function setTime() {
		++totalSeconds;
		secondsLabel.innerHTML = pad(totalSeconds % 60);
		minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60) % 60);
		hoursLabel.innerHTML = pad(parseInt(totalSeconds / 3600) % 24);
		daysLabel.innerHTML = pad(parseInt(totalSeconds / 86400));
	}

	function pad(val) {
		var valString = val + "";
		if (valString.length < 2) {
			return "0" + valString;
		} else {
			return valString;
		}
	}
</script>