Hi all!

I have this code. Everything works fine except "setTimeout..." function. It's supposed to repeat this function every second (and change the age). Can someone, anyone, help me? Thanks a lot!

<script type="text/javascript">
function parseDate(input){
	var parts = input.match(/(\d+)/g);
	return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4]);
}
function calculate(){
	var date = new Date();
	var min = document.getElementById('minute').value;
	var hour = document.getElementById('hour').value;
	var day = document.getElementById('day').value;
	var mon = document.getElementById('month').value;
	var year = document.getElementById('year').value;
	var cursec = date.getSeconds();
	var curmin = date.getMinutes();
	var curhour = date.getHours()
	var curday = date.getDate();
	var curmon = date.getMonth()+1;
	var curyear = date.getFullYear();
	var therest1_ms = cursec*1000+curmin*60*1000+curhour*60*60*1000;
	var therest2_ms = min*60*1000+hour*60*60*1000;
	var one_day = 1000*60*60*24;
	if(curmon>=mon && curday>=day && curhour>=hour && curmin>=min) year_o=curyear-year;
	else year_o=curyear-year-1;
	if((curmon==9 && curday>=7) || curmon>9){
		var date1 = parseDate(curyear+"/"+curmon+"/"+curday+"/"+curhour+"/"+curmin+"/"+cursec);
		var date2 = parseDate(curyear+"/"+mon+"/"+day+"/"+hour+"/"+min);
	}else{
		var date1 = parseDate(curyear+"/"+curmon+"/"+curday+"/"+curhour+"/"+curmin+"/"+cursec);
		var date2 = parseDate((curyear-1)+"/"+mon+"/"+day+"/"+hour+"/"+min);
	}
	var date1_ms = date1.getTime()+therest1_ms;
	var date2_ms = date2.getTime()+therest2_ms;
	var diff_ms = date1_ms-date2_ms;
	var diff = diff_ms/one_day;
	if(curyear%100==0){
		if((curyear/100)%4==0) divide = 366;
		else divide = 365;
	}else if(curyear%4==0) divide = 366;
		else divide = 365;
	years_old=year_o+diff/divide;
	document.getElementById('divid').innerHTML=years_old;
	setTimeout(calculate(),1000);///???????
}
</script>
<div id="divid"></div>
Date of birth:<br>
Minutes (0-59): <input type="text" id="minute" size="2" maxlength="2"> Hours (0-23): <input type="text" id="hour" size="2" maxlength="2"> Day (1-31): <input type="text" id="day" size="2" maxlength="2"> Month (1-12): <input type="text" id="month" size="2" maxlength="2"> Year (19xx): <input type="text" id="year" size="4" maxlength="4">
<input type="button" value="Calculate" onclick="calculate()">
setTimeout(calculate,1000);

In a setTimeout() or setInterval() statement, either define an anonymous function or provide a reference to a named function, but (typically) don't execute it.

Execution happens when the time period has expired.

"Typically" above: Javascript functions are first class objects. You might one day encounter a function that returns a function, in which case you might see a function call inside a setTimeout() or setInterval() statement.

Airshow

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.