I have got a piece of code, that should countdown some number (in this example 1111) and write it into span id=”timeframe”. But it doesn’t work, i dont know why. Can you tell me where is my mistake, please?
<script language="JavaScript">
<!--
var txt_minute = "Min";
var txt_second = "s";
var time_left = 1111;
var temp=timeleft;
houres = Math.floor(temp / 3600);
temp %= 3600;
minutes = Math.floor(temp / 60)";
temp %= 60;
seconds= temp;
function display()
{
time_left -= 1;
if (seconds <= 0) {
seconds = 59;
minutes -= 1;
} else {
seconds -= 1;
}
if (minutes <= 0){
minutes=59;
houres-=1;
}
if (time_left <= 0) {
seconds = 0;
}
if (time_left > 0) {
document.getElementById("timeframe").innerHTML = houres +" hours" + minutes + " " + txt_minute + " " + seconds + " " + txt_second;
} else {
window.location.reload();
}
setTimeout("display()",1000);
}
display();
//-->
</script>
There are some smaller mistakes:
minutes = Math.floor(temp / 60)";There is a trailing quotation mark that shouldn’t be there.
var temp=timeleft;That should be
time_left.setTimeout(display,1000);edit: I wrote a small, clearer countdown script.
Just alter the
updateandcompletedfunction to your needs and start the countdown with your desired value.And btw. if you want to change some
innerHTMLof an element in your code, you should save the reference somewhere, instead of getting it via DOM every call.