Reputation: 47
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
Upvotes: 3
Views: 330
Reputation: 113906
As with any async code, IAP_loaded
is indeed changed to true but not before the code at the bottom executes. To check this, add this test at the bottom after 4:
(function check_IAP(){
console.log("5] -- IAP Loaded: "+IAP_loaded);
if (!IAP_loaded) {
setTimeout(check_IAP,500);
}
)();
Upvotes: 0
Reputation: 1168
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
4] -- IAP Loaded: false
2] -- IAP Loaded: false
3] -- IAP Loaded: true
1] -- IAP Loaded: true
Please try to provide us the complete code, as how the function is called etc.
Upvotes: 1