I've got some javascript here to calculate running totals in hours of time logged in <select> elements.
Don't worry about figuring the code-- I know it works. My problem is that I want the totals calculated and this requires the use of an array from the OTHER function...
So in addTimesIN() i use timeins_dec[]Code:// -----------------FOR TIME LOG---------------------------- // arrays needed timeins = new Array(); timeins_split = new Array(); timeins_min = new Array(); timeins_dec = new Array(); timeouts = new Array(); timeouts_split = new Array(); timeouts_min = new Array(); timeouts_dec = new Array(); totaltimes = new Array(); dec_total = new Array(); // make the onchange events for (y = 1; y <= 4; y++) { document.getElementById('timein' + y).onchange = addTimesIN; document.getElementById('timeout' + y).onchange = addTimesOUT; } function addTimesIN() { // set variables when onchange is triggered timeins[y] = this.options[this.selectedIndex].value; // split the variables into mathematically workable stuff timeins_split[y] = timeins[y].split(":"); // re-assemble as decimals timeins_min[y] = timeins_split[y][1] / 60; //works... timeins_dec[y] = (parseInt(timeins_split[y][0]) + timeins_min[y]); console.log('Time in: ' + timeins_dec[y]); // calculate that day's total dec_total[1] = (parseInt(timeouts_dec[1]) - parseInt(timeins_dec[1])); // set it to the totals box document.getElementById('totaltime1').value = dec_total[1]; // add all totals var all_time_totals = dec_total[1] + dec_total[2] + dec_total[3] + dec_total[4]; document.getElementById('alltimetotal').value = all_time_totals; } function addTimesOUT() { // set variables when onchange is triggered timeouts[y] = this.options[this.selectedIndex].value; // split the variables into mathematically workable stuff timeouts_split[y] = timeouts[y].split(":"); // re-assemble as decimals timeouts_min[y] = timeouts_split[y][1] / 60; timeouts_dec[y] = (parseInt(timeouts_split[y][0]) + timeouts_min[y]); console.log('Time Out: ' + timeouts_dec[y]); // calculate that day's total dec_total[1] = (parseInt(timeouts_dec[1]) - parseInt(timeins_dec[1])); // set it to the totals box document.getElementById('totaltime1').value = dec_total[1]; // add all totals var all_time_totals = dec_total[1] + dec_total[2] + dec_total[3] + dec_total[4]; document.getElementById('alltimetotal').value = all_time_totals; } // ------------------END TIME LOG--------------
and in addTimesOUT() i use timeouts_dec[]
but because they are in separate functions those arrays aren't being recognized....


Reply With Quote
Bookmarks