SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question weird variable scoping problem

    Hi

    Have been programming an javascript for a number of years now, and just started to get involved with jQuery, and I've stumbled across a problem which has really got me stumped.

    Code:
    function calc_calculate ()
    {
    	// empty all quotation areas
    	$("#quotation-aggregates").empty();
    	$("#quotation-stones").empty()
    	$("#quotation-total-discount").empty()
    	$("#quotation-total-collect").empty()
    	extras = calc_get_extras ();
    	aggstotal = 0;
    	agglist = $("<ul />");
    	if(extras['cement']['retail'])
    	{
    		aggstotal+=extras['cement']['retail'];
    		agglist.append($("<li />").append("Bags of cement: "+extras['cement']['bags']+" (&pound;"+extras['cement']['retail']+")"));
    	}
    	if(extras['sharpSand']['retail'])
    	{
    		aggstotal+=extras['sharpSand']['retail'];
    		aggstotal+=extras['buildingSand']['retail'];
    		agglist.append($("<li />").append("Bags of sharp sand: "+extras['sharpSand']['bags']+" (&pound;"+extras['sharpSand']['retail']+")"));
    		agglist.append($("<li />").append("Bags of building sand: "+extras['buildingSand']['bags']+" (&pound;"+extras['buildingSand']['retail']+")"));
    	}
    	stoneslist = $("<ul />");
    	var stonestotal=0;
    	$.post("/legacy/costCalculatorResults.php", {
    				colour: $("#calc-colour input:checked").attr("value"),
    				patio_size: $("#calc-size-input").attr("value"),
    				pattern: $("#calc-pattern input:checked").attr("value"),
    				paving_variety: $("#calc-variety-select").val()},
    			function(data){
    				stones = data.split ("\n");
    				for(i=0;i<stones.length;i++)
    				{
    					if (stones[i].length)
    					{
    						details = stones[i].split(",");
    						stoneslist.append($("<li />").append(details[0] + ": " + details[1] + " (&pound;"+details[2]+")"));
    						stonestotal+=Number(details[2]);
    					}
    				}
    			});
    
    	stonestotal=(Math.round(100*stonestotal)/100);
            // alert (stonestotal)
    	$("#quotation-stones").append(stoneslist);
    	$("#quotation-aggregates").append(agglist);
    	$("#quotation-total").append(stonestotal);
    }
    The problem variable is stonestotal. for some reason, it's value is 0, UNLESS i uncomment out the alert 4 lines from the bottom, in which case the alert says 0, then the correct value is appended to the document.

    Any ideas on why this might be happening? Seems very weird to me that the scope of the variable changes after it gets used in the alert function.

    Any suggestions of how else to get this variable out of the anonymous function would also be useful!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,191
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    divide

    Hi slightlymore, welcome to the forums,
    I see
    Code:
    	// empty all quotation areas
    	$("#quotation-aggregates").empty();
    	$("#quotation-stones").empty()
    	$("#quotation-total-discount").empty()
    	$("#quotation-total-collect").empty()
    and
    Code:
    	$("#quotation-stones").append(stoneslist);
    	$("#quotation-aggregates").append(agglist);
    	$("#quotation-total").append(stonestotal);
    Is "total", w/o "discount/collect", also getting empty()ed?
    If stonestotal is 0, can 0 be divided by 100 without breaking the code?

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    UNLESS i uncomment out the alert 4 lines from the bottom, in which case the alert says 0, then the correct value is appended to the document
    That part sounds like you are calling the function before the elements in question exist, but when you put in the alert it gives the page time to finish loading so the elements do exist.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •