0

Ok I've looked at a few questions and am now confused.

I have a code say:

var basket = null;

function loadBasket(){
    // do somesthing
    basket = "something";
    console.log(basket);
    load();
}

function load(){
    // do something with basket variable
}

loadBasket();

The load function seems to return null for basket even though I've set it previously in the loadBasket() function. The only thing I can think of is the order in which I've called the functions. I've got similar functions on other sites that work fine, this one doesn't though. Is it the order is there something I'm missing. Could someone please explain, if they don't mind doing so.

The console log returns whatever I have in place of "something", so why is the following function not recognising the newly changed variable.

edit:

ok i set it up because i'm not clear on callbacks and am using post requests. so real function is simular to this.

var basketId = readCookie('basket');

function loadBasket(){
    $.post("test.php", {"email":email}, function(data){
         if(data = 0){
             basketId = readCookie('basket');
         }
         if(data == null){
             basketId = data;
         }
         else{
             basketId = data;
             console.log(basketId);
             load();
         }
    });
}

function load()
    if(basketId == null){
        console.log(basketId);
        //do something
    }
    else{
        $.post('basket.php', {"basketId":basketId}
    }
}

pageLoad();

it's a little more complex but the idea is the same, iI would have though it would work, but it doesn't even though console.log shows data for basketId. In the load section it acts as if basket is null even though the other console.log shows basketId changing. Like I said I've got other functions on other pages that are the same but work.

edit 2:

other function that works:

var basketId = readCookie("basket");

function loadTables(){
   if(something){
       $.post("test.php", {"basketId":basketId}, function(data){
          //do something with data
       });
   }
   else{
       //do something
   }
}

function loginCheck(){
     if(something){
        $.post("test2.php", {"email":email}, function(data2){
           if(something){
                createCookie('basket', data2, 0);
                basketId = data2;
                loadTables();
           }
           else{
                return false;
           }
        });
     }
     else{
          return false;
     }
 }

 loginCheck();
Tom Albanese
  • 173
  • 1
  • 11
  • How are you calling `load` and `loadBasket`? – MinusFour Jan 23 '16 at 17:58
  • sorry I'll edit the questions – Tom Albanese Jan 23 '16 at 17:58
  • You will need to show what is in your `load` function – Patrick Evans Jan 23 '16 at 18:02
  • 1
    http://jsbin.com/koxeqi/1/edit?js,console — cannot reproduce – Quentin Jan 23 '16 at 18:02
  • _"The load function seems to return null for basket "_ No value appear returned from `load()` ? _"The console log returns whatever I have in place of "something", so why is the following function not recognising the newly changed variable"_ Is `console.log(basket)` called within `load()` ?, or on next line after `loadBasket()` is called ? Can create stacksnippets https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/ to demonstrate ? – guest271314 Jan 23 '16 at 18:04
  • 1
    @HelloWorld How would using an IIFE change results described at OP ? – guest271314 Jan 23 '16 at 18:09
  • @HelloWorld — As opposed to when they aren't in an IIFE and so are in the same (global) scope… – Quentin Jan 23 '16 at 18:14
  • 1
    Re: The edit to the question. Paulpro's duplicate close was correct. – Quentin Jan 23 '16 at 18:17
  • @TomAlbanese Tried passing `basketId` to `load` as a parameter `load(basketId)` ? – guest271314 Jan 23 '16 at 18:18
  • I could do that but why doesn't this work. I mean I will try that, but on other functions that are simular it works. should I change them even though they work – Tom Albanese Jan 23 '16 at 18:23
  • @TomAlbanese Callback of `$.post()` is asynchronous . Where is `pageLoad` defined ? – guest271314 Jan 23 '16 at 18:26
  • pageLoad is defined on it's own designed to load on page load. Like I said I've done similar things before and they work, surely calling the function on completion of the request means that it should now use updated basketId. What really puzzles me is why similar functions work. – Tom Albanese Jan 23 '16 at 18:36
  • I need to know this, my whole site does a similar thing of setting global variables then using them in functions. Why would this example be different. I've also got similar post requests that I don't want breaking one day. – Tom Albanese Jan 23 '16 at 18:40
  • @TomAlbanese _"Why would this example be different."_ See condition at `if(data = 0)` ? which sets `data` to `0` ? Try at `console` : `var data; if (data = 0) { console.log("if", data) } else { console.log("else", data) }` . Try changing operator to `==` or `===` – guest271314 Jan 23 '16 at 18:45
  • nah, thats a typo. On closer inspection it is something to do with callback, I think. I have another post request that deals with data in a similar way and that isn't working either. I fixed it with parameters, but still have another function that does the same thing but was working fine. The only thing I can think of is that it uses a similar readCookie statement, but that one actually creates a cookie before calling the function. It's still in the oncompletion section it just doesn't use the data that has been retrived. I'll edit to show. – Tom Albanese Jan 23 '16 at 18:50
  • @TomAlbanese _"nah, thats a typo."_ Ok; what is actual operator used at `if` condition ? Can create jsfiddle http://jsfiddle.net to reproduce issue described at OP ? – guest271314 Jan 23 '16 at 18:51
  • it should be "==". I don't use jsfiddle, does it work with post requests – Tom Albanese Jan 23 '16 at 19:02
  • Yes, jsfiddle works with post requests. Entire `js` at Question could be reproduced , see http://doc.jsfiddle.net/use/echo.html . Could also use http://plnkr.co – guest271314 Jan 23 '16 at 19:04
  • @TomAlbanese Tried reproducing _"The load function seems to return null for basket even though I've set it previously in the loadBasket() function."_ at jsfiddle ? – guest271314 Jan 23 '16 at 19:22
  • no i've got housework to do – Tom Albanese Jan 23 '16 at 19:26

0 Answers0