Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Member
    Join Date
    Jun 2007
    Location
    Scotts Valley, CA
    Posts
    49
    Member #
    15405
    Hi all,

    I'm using the following script to some elements across rows in a form. Problem is that it really slows down entry of an order, as the form contains maybe a thousand different products. Here's the script:

    Code:
    function sumQuantities()
    {
    i=0;
    while(i < document.orderForm.elements.length)
        {
        if((i%16==0)&&(i!=0))
            {
            var j = document.orderForm.elements[i-1].value-0;
            var k = document.orderForm.elements[i-2].value-0;
            var a = document.orderForm.elements[i-3].value-0;
            var b = document.orderForm.elements[i-4].value-0;
            var c = document.orderForm.elements[i-5].value-0;
            var d = document.orderForm.elements[i-6].value-0;
            var e = document.orderForm.elements[i-7].value-0;
            var f = document.orderForm.elements[i-8].value-0;
            var g = document.orderForm.elements[i-9].value-0;
            var h = document.orderForm.elements[i-10].value-0;
            var l = document.orderForm.elements[i-11].value-0;
            var m = document.orderForm.elements[i-12].value-0;
            
            document.orderForm.elements[i-3].value = b + c + d + e + f + g + h + l + m;
            document.orderForm.elements[i-1].value = tidy((b + c + d + e + f + g + h + l + m)*k);
            }
    
        i++;
        }
    }
    Any ideas about how I could speed things up?

    Cheers,

    K.

  2.  

  3. #2
    Senior Member hyperair's Avatar
    Join Date
    May 2005
    Posts
    452
    Member #
    9892
    Use parseInt() to convert a string into an integer, and parseFloat() to convert a string into a floating point number. In your case:

    Code:
    var j = parseInt(document.orderForm.elements[i-1].value);
    And repeat the change for the rest of them.

    Also, could you show what's the contents of your tidy() function? Perhaps optimizations can be done there as well.

    Another thing you could do is save the form's elements to a variable. Then you won't have to refer through document so often.

    Code:
    var elms = document.orderForm.elements;
    var j = parseInt(elms[i-1].value);
    ...
    Hyperair

  4. #3
    Member
    Join Date
    Jun 2007
    Location
    Scotts Valley, CA
    Posts
    49
    Member #
    15405
    Hi Hyperair,

    Thanks for your help. The use of a variable for the form elements seems to help a bit. I'm having some trouble, though, getting the parseInt function to work. I'm consistently getting an NaN error when I use it. Any ideas on why that might be?

    Cheers,

    K.

  5. #4
    Senior Member hyperair's Avatar
    Join Date
    May 2005
    Posts
    452
    Member #
    9892
    Whoops sorry I forgot that parseInt cannot handle blank strings or strings with utterly no numbers in them. You'll have to do a check for blank strings and change them to 0. I reckon it could end up taking more CPU so if it does you should just go back to the old. However, I reckon it would be worth testing.
    Hyperair

  6. #5
    Member
    Join Date
    Jun 2007
    Location
    Scotts Valley, CA
    Posts
    49
    Member #
    15405
    Hi all,

    Just wanted to report back. I've got it set up so that we start with a value of 0 for all inputs before the customer starts entering their order, and that's resolved the parseInt issue. The code I'm using runs like this:

    Code:
    var elms = document.orderForm.elements;
    var elmslength = elms.length;
    function sumUnits()
        {
        for( i=1; i<elmslength; i++)
            {
            if((i%15==0))
                {
                var a = parseFloat(elms[i-12].value);
                var b = parseFloat(elms[i-11].value);
                var c = parseFloat(elms[i-10].value);
                var d = parseFloat(elms[i-9].value);
                var e = parseFloat(elms[i-8].value);
                var f = parseFloat(elms[i-7].value);
                var g = parseFloat(elms[i-6].value);
                var h = parseFloat(elms[i-5].value);
                var k = parseFloat(elms[i-4].value);
                    
                elms[i-3].value = a + b + c + d + e + f + g + h + k;
                elms[i-1].value = parseFloat(elms[i-3].value) * parseFloat(elms[i-2].value);
                }
            } 
        }
    I'm declaring the elms and elmslength variables outside the function, because I'm reusing them in my validation functions.

    Cheers,

    K.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:08 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com