Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript global variable not staying changed outside callback function

var IAP_loaded = false; // Global Scope

var IAP_onReady = function(){
     storekit.load(IAP_list, function (products, invalidIds) {
            IAP_loaded = true;
            console.log("1] -- IAP Loaded: "+IAP_loaded); // Outputs true
     });
     console.log("2] -- IAP Loaded: "+IAP_loaded); // Outputs false
     IAP_loaded = true;
     console.log("3] -- IAP Loaded: "+IAP_loaded); // Outputs true
};

console.log("4] -- IAP Loaded: "+IAP_loaded); // Outputs false

For some my global variables aren't staying modified outside the function itself... I created a completely separate function and changed a global variable and it works fine. This just mind baffles me. Any thoughts?

PS. This is a Phonegap 3.0 project and the callback function is from IAP plugin. Found here https://github.com/j3k0/PhoneGap-InAppPurchase-iOS

storekit.load is Asynchronous! Thanks for narrowing it.

2] -- IAP Loaded: false 3] -- IAP Loaded: true 4] -- IAP Loaded: false 1] -- IAP Loaded: true

I get back the response in that order. But it is not Ajax. It's through Objective C, the javascript just handles the responses so it's editable through javascript

like image 860
Mosayed Avatar asked Dec 04 '25 10:12

Mosayed


1 Answers

As suggested IAP (in app purchase) works similar to Ajax. In fact most of phonegap plugins follow the same pattern.

First lets see how plugins works - A call from JS is sent to Obj C file - While Obj C is doing desired calculations other JS code and run simultaneously - After Obj C has completed its calculations the output is sent back to JS via callback

Now lets try to see the code you have provided

var IAP_loaded = false; // Global Scope

var IAP_onReady = function(){
     storekit.load(IAP_list, function (products, invalidIds) {
            IAP_loaded = true;
            console.log("1] -- IAP Loaded: "+IAP_loaded); // Outputs true
     });
     console.log("2] -- IAP Loaded: "+IAP_loaded); // Outputs false
     IAP_loaded = true;
     console.log("3] -- IAP Loaded: "+IAP_loaded); // Outputs true
};

console.log("4] -- IAP Loaded: "+IAP_loaded); // Outputs false

Note - I'm referring to console.log as logs below

Now javascript runs the file is this order

You must be calling 'IAP_onReady' from somewhere, hence

  • log 4 is printed instantly as it is a non-asyn code with output of 'IAP_loaded' as false. You have initiallized 'IAP_loaded' as false

4] -- IAP Loaded: false

  • log 2 is printed instantly as it is a non-asyn code with output of var as false, as still 'IAP_loaded' hasn't been modified yet

2] -- IAP Loaded: false

  • log 3 is printed instantly as it is a non-asyn code, but you have changed the value of 'IAP_loaded' manually to true

3] -- IAP Loaded: true

  • Now after the execution of 'storekit.load' has completed log 1 is printed, and as here value of 'IAP_loaded' manually to true

1] -- IAP Loaded: true

Please try to provide us the complete code, as how the function is called etc.

like image 147
Siddhartha Gupta Avatar answered Dec 07 '25 01:12

Siddhartha Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!