Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update value based on time selected in another field

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
GB
Hi,

I have a field that diplays time in the format

00:00:00

I also have another field that will hold a cost value based on the amount of time spent on the call

i.e. for instance

if time spent does not = 03:00:00 then cost equals £30
then for every part hour after that the cost increases by £10

so if the time = 03:10:00 then the cost will be £40.

can anyone explain some javascript code that I can use to do this so that onchange will populate the cost based on the time value.

any help would be much apopreciated

Regards

Paul

 
In this example, I'm assuming 3:09 is the same as 3:00 in terms of billing... I'm also not using form fields, but table cells to display time and cost.

Javascript first:
Code:
<script type="text/javascript">
var INITIAL_COST = 30;
var INCREMENTAL_COST = 10;
var interval = -1;
var hours = 0;
var minutes = 0;
var totalMinutes = 0;
var totalCost = 0;
function startTimer() {
	alert("Go");
	interval = setInterval(setFields, 60000); //time in milliseconds
}
function stopTimer() {
	alert("Stop");
	clearInterval(interval);
}
function setFields() {
	totalMinutes++;
	minutes++;
	if(minutes >= 60) {
		hours++;
		minutes = 0;
	}
	timeStr = "";
	if(minutes < 10) {
		timeStr = "0"
	}
	timeStr = hours+":"+timeStr + minutes + ":00";
	if(hours < 10) {
		timeStr = "0"+timeStr;
	}
	
	if(totalMinutes == 180) {
		totalCost = INITIAL_COST;
	} else if(totalMinutes > 180 && totalMinutes != 0 && totalMinutes%10 == 0) {
		totalCost = (totalCost*1)+(INCREMENTAL_COST*1)
	}
	
	document.getElementById("time_td").innerHTML = timeStr;
	document.getElementById("cost_td").innerHTML = totalCost;
}
</script>

Now html:
Code:
<table cellspacing="0" cellpadding="2">
	<tr>
		<td>Time Spent:</td>
		<td id="time_td">00:00:00</td>
	</tr>
	<tr>
		<td>Total Cost:</td>
		<td id="cost_td">0</td>
	</tr>
	<tr>
		<Td colspan = 2>
			<input type="button" name="go" value="go" onclick="startTimer();">
		</td>
	</tr>
	<tr>
		<Td colspan = 2>
			<input type="button" name="stop" value="stop" onclick="stopTimer();">
		</td>
	</tr>
</table>

I also didn't thouroughly check the code for errors, but it should be error free.

 
okiiyama,

thanks for the response, I just read my thread again and some info was wrong,

If the time is between 00:00:00 and 03:00:00 then the cost is £30

if the time was between 03:01:00 and 04:00:00 the cost would be £40

also is there any way to update the field without using the buttons for the timer

Regards

Paul

 
I'm generally a lazy problem solver which in the long run tends to makes my solutions more complicated or longer, but I think I solved this one.

1. Instead of a button to start the time you can use:
Code:
<body onload="startTimer();">

2. I changed the JS so that:
a. time <= 180 minutes = £30
b. time "in between hours" ex 04:01:00 - 05:00:00 will be incremented only once... (This is what "isInBetween" is used for).
Code:
<script type="text/javascript">
var INITIAL_COST = 30;
var INCREMENTAL_COST = 10;
var interval = -1;
var hours = 0;
var minutes = 0;
var totalMinutes = 0;
var totalCost = 0;
var isInBetween = false;
function startTimer() {
	alert("Go");
	interval = setInterval(setFields, 60000); //time in milliseconds
}
function stopTimer() {
	alert("Stop");
	clearInterval(interval);
}
function setFields() {
	totalMinutes++;
	minutes++;
	if(minutes >= 60) {
		hours++;
		minutes = 0;
	}
	timeStr = "";
	if(minutes < 10) {
		timeStr = "0"
	}
	timeStr = hours+":"+timeStr + minutes + ":00";
	if(hours < 10) {
		timeStr = "0"+timeStr;
	}
	
	if(totalMinutes <= 180) {
		totalCost = INITIAL_COST;
		isInBetween = true;
	} else if(isInBetween) {
		totalCost += INCREMENTAL_COST
		isInBetween = false;
	} else if(totalMinutes%60==0) {
		isInBetween = true;
	}
	
	document.getElementById("time_td").innerHTML = timeStr;
	document.getElementById("cost_td").innerHTML = totalCost;
}
</script>

I left the function "stopTimer();" in you can remove that as well as the form buttons.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top