[thelist] Javascript orderform speed issue

Rory Lysaght rorylysaght at gmail.com
Sun Mar 5 18:38:16 CST 2006


Matt,
  Thanks.  You were right - I don't need to cycle through the entire
form every single time.  I just check the changed field and add it to
the subtotal.  Script zips along now in Firefox and IE.
EXCEPT for one situation:  if someone enters a bunch of items, then
goes back and deletes a previously entered number, the script has no
way to tell what quantity was just deleted and to update the total. 
So in this case, I need to recalculate the entire form again.  I moved
my old loop out into a function, so I only need to run it in this
case, but then I'm back to glacial performance in IE.


On 3/5/06, Matt Warden <mwarden at gmail.com> wrote:
> There's no need to go through every field. Only the ones the user has
> touched.
>
> Something like...
>
> var touched = {};
>
> window.onload = function()
> {
>         var inputs = document.getElementsByTagName('input');
>         for (var i=0; i<inputs.length; i++) {
>                 inputs[i].onchange = markTouched;
>         }
> }
>
> function markTouched(e)
> {
>         e = e || window.event;
>         var input = e.srcElement || e.target;
>         var id = input.id;
>         touched[id] = input.value;
> }
>
> function updateTotal()
> {
>         var totalinput  = ...;
>         // ...
>         // use the object touched = {id: value, id: value, etc.}
>         // to calculate total. you don't really need to revisit
>         // the actual input fields at this point.
>         // ...
> }
>
> window.setInterval("updateTotal();", 800);
>
> - --
> Matt Warden
> Miami University
> Oxford, OH, USA
> http://mattwarden.com



More information about the thelist mailing list