SageFix. LetsFix!  
Current Location : Home > Web Development > JavaScript > Variable scope has got me confused Share/Save/Bookmark    

Main Menu
SageFix - Let's Fix

Board Categories
    Web Site Designing
- Graphics
    Web Development
- .NET
- Classic ASP
- JavaScript
- PHP
    WebSite Management
- Promotion Techniques
- Search Engine Optimization
    Databases
- MySQL

Tutorials

Database Connection Strings

  Contact Us


 1 - 3 of 3 [Total 1 Pages]  
Variable scope has got me confused      Jul 16, 2009, 05:38 1
  
I really find it difficult getting to grips with JavaScript's variable scope. It never seems that consistent... probably because I never understand it right. Anyway...

I'm trying to build a simple price calculator. The code is supposed to loop through a bunch of <select> boxes, send an AJAX query to get the price for the currently selected product, adding that amount onto the total each time, before outputting it into an <input type="text" /> element further down the page.

Onto the code:

JavaScript Code:
$("#course_calculate").click(function() {
 
    var totalCost = Number(0);
 
    $("#course_dropdown_container select").each(function(i) {
        if(this.value != 0) {
            $.get("/_cms/admin/ajax/sku_get_price.php", { id: this.value }, function(data) {
                totalCost = totalCost + Number(data);
            });
        }
    });
 
    $("#booking_total_cost").attr("value", totalCost.toFixed(2));
 
});

If I alert() the value of totalCost right after a new price has been added, it works as expected. The value of totalCost, however, doesn't seem to persist beyond the each() function, and as such, referring to totalCost at the end of the script just outputs zero.

If anyone could point me in the right direction here I'd be very grateful

 
      Jul 16, 2009, 08:10 2
  
This isn't a scoping problem.

$.get uses an asynchronous http request. The key thing here is, that javascript doesn't wait for the request to complete. Your loop finishes immediately, but the actual data isn't retrieved until later(and your callback function won't get called until its specific request is complete.

You could use $.ajax and then set it to not use an asynchronous request.

But really, you should just grab all the prices in a single asynchronous request if possible. Then have your callback function sum them, and update the dom with the result.

 
      Jul 16, 2009, 08:22 3
  
Quote:
But really, you should just grab all the prices in a single asynchronous request if possible. Then have your callback function sum them, and update the dom with the result.
Huh, no idea why I didn't even think of this. Seems like a much more sensible approach! Thanks for replying

 
 1 - 3 of 3 [Total 1 Pages]  









Subscribe to our mailing list
email


Current Location : Home > Web Development > JavaScript > Variable scope has got me confused Share/Save/Bookmark    

Development problem? SageFix is completely free -- paid for by advertisers and donations. Click here to Contact Us If you have any query. Enjoy!
Request processed in 7.0E-5 seconds Home - Contact Us - Terms Of Service - Privacy Policy - Copyrights - Top
Advertisements do not imply our endorsement of that product or service.
Current server time now is 06-Sep-2010 06:15:14
Copyright © 2009 Sagefix Inc. All rights reserved.
Powered By SageFix