
Question on adding a long list of numbers?
Raymond,
Do you have really 400+ entry fields on your form, and a couple of script
lines for each block?
The script would be more manageable if you use a loop to iterate through
all fields. It would shorten the script considerably, as well as reduce the
number of variables used.
Some small tips, that might or might not accelerate:
- use " ! isNaN(subtotal) " instead of " isNaN(subtotal) == false "
- why the check "subtotal != 0" ? If it IS 0, then the total won't be
affected!
- try " Total += SubTotal " instead of " Total = Total + SubTotal "
- are you really calling showTotal again at the end of the function?
If you want to show the current total as the fields are filled in,
try using the "onChange" event to only add changed values one at a time
to the total, maybe together with "onFocus" to remember the old value
(so you can subtract that from the total)
Hans Kesting
Quote:
> Hi all,
> I have following JavaScript function run in my page, as you can see this
> works fine with just few SubTotals, but my SubTotals gets very large,
about
> 400+, and this get quite slow, my question is, any way to make this
faster?
> may be is because of the Total = Total + SubTotals?
> function showTotal()
> {
> var Total = 0;
> var SubTotal1;
> SubTotal1 = parseFloat(document.my_form.amount1.value);
> if ((isNaN(SubTotal1) == false)&&(SubTotal1 != 0))
> {
> Total = Total + SubTotal1;
> }
> var SubTotal2;
> SubTotal2 = parseFloat(document.my_form.amount2.value);
> if ((isNaN(SubTotal2) == false)&&(SubTotal2 != 0))
> {
> Total = Total + SubTotal2;
> }
> ....
> document.AR_invoice_update.total_paid.value = Total;
> setTimeout("showTotal()",300);
> }
> Thanks for help
> Raymond Yap