Timer lag in Firefox

Hi,

I have the following code for a countdown timer:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Test</title>
</head>
<body>
	<div id="timer"></div>
	<script>
	var currentTime = new Date().getTime();
	var days = 11;
	var hours = 6;
	var minutes = 32;
	var seconds = 44;
	var targetTime = currentTime + (days * 24 * 60 * 60 * 1000) + (hours * 60 * 60 * 1000) + (minutes * 60 * 1000) + (seconds * 1000);
	
	setInterval(function() {
		var currentNewTime = new Date().getTime();
		var secondsLeft = (targetTime - currentNewTime) / 1000;
		var days = parseInt(secondsLeft / 86400);
		days = (days < 10) ? "0"+days : days;
		secondsLeft = secondsLeft % 86400;
		var hours = parseInt(secondsLeft / 3600);
		hours = (hours < 10) ? "0"+hours : hours;
		secondsLeft = secondsLeft % 3600;
		var minutes = parseInt(secondsLeft / 60);
		minutes = (minutes < 10) ? "0"+minutes : minutes;
		var seconds = parseInt(secondsLeft % 60);
		seconds = (seconds < 10) ? "0"+seconds : seconds;
		if (secondsLeft > 0) {
			document.getElementById("timer").innerHTML = days+":"+hours+":"+minutes+":"+seconds;
		}
	}, 1000);
	</script>
</body>
</html>

It works fine on Chrome but it lags on Firefox. Every 5-10 seconds, it stops, then it jumps 2 seconds down. Any idea what might cause that and how to fix it?

Thanks.

I can’t answer why this is happening, but on my computer (Windows Vista SP2 :soon: :skull: ), I get the lag on FF 32.0.2, IE 9, Opera 12.17 and Google Chrome 37.

I changed the timer to count down from 1 minute 1 second, and it starts at 58 seconds then it will freeze and do the 2-4 second jump several times.

Thank you for testing on other browsers :slight_smile:

I then found a sample timer at

and it seems to be working better.