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();