0

I've been trying this for a while. Although there are alot of similar questions on stack, none of them have seemed to work for me. Here is my issue

Here's my code and this was my latest attempt

var busWorkOrders;

function jsonBusWorkOrders() {
    busWorkOrders = "New Value";
}

How can busWorkOrders new value of "New Value" be accessed outside of the function jsonBusWorkOrders().

I have also tried How do I change the value of a global variable inside of a function

Any help would be appreciated.

Community
  • 1
  • 1
plgelso
  • 107
  • 2
  • 15
  • 2
    What is the actual problem that you're having? – Adrian May 18 '17 at 14:08
  • Why does it need to be a global variable? Why is `showResults` not a function at the same level as well? So confusing. – Dave Newton May 18 '17 at 14:08
  • I'd up vote @Adrian comment twice if I could. Rather than trying to solve the convoluted structure you've created, take a step back and describe the thing you're trying to do (and why you're trying to do it) in your question. – Adam Jenkins May 18 '17 at 14:10
  • You can User localStorage of browser to keep value and then access that value when your document is ready. – Rahul May 18 '17 at 14:11
  • busWorkOrders is initially set to undefined. I want to reassign busWorkOrders to the json value pass in the function jsonBusWorkOrders(). Then use that value for busWorkOrders in document.ready() @DaveNewton – plgelso May 18 '17 at 14:11
  • @PeterGelsomino - **when** is `jsonBusWorkOrders` called? This smells to me like you're mishandling an asynchronous process. – Adam Jenkins May 18 '17 at 14:15
  • @Adam i edited the question to make it simpler to understand. jsonBusWorkOrders gets triggered by a native function in swift. Using alerts iv confirmed jsonBusWorkOrders gets called before document.ready() which is good – plgelso May 18 '17 at 14:18
  • @PeterGelsomino The code you posted (in your edit) successfully modified the global variable `busWorkOrders`. If you console.log `busWorkOrders` before you call your function, then call your function, and then after you call your function, you'll see the `busWorkOrders` has actually been modified (reassigned), so that's no longer your problem (nor was it ever). I imagine now your problem is how to let Swift know that your variable now contains the information it requires. – Adam Jenkins May 18 '17 at 14:21

2 Answers2

0

Here's my guess:

When the page loads, you don't have busWorkOrders - so set it to an empty array:

//global scope
var busWorkOrders = [];

Then, you call some asynchronous process to populate busWorkOrders (probably like this):

someProcess().then(jsonBusWorkOrders)

What you actually want to do is to modify busWorkOrders - not replace it (although it probably doesn't matter a whole lot).

//global scope
var busWorkOrders = [];
someProcess().then(function(response) {
  busWorkOrders.splice.apply([0,0].concat(response));
});

In addition, you may have to let whatever is waiting for busWorkOrders to be populated that it's now ready. So now my question is, what is waiting on busWorkOrders to be populated.

Keep in mind, Swift won't keep track of changes to the variable after it gets a hold of it, so you probably want to let Swift know - 'the contents of my variable have changed, now go and get it again'

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • Absolutely nailed it on the head. I asked a senior developer and he explained my problem. Basically, Swift was sending a request to JavaScript. jsonBusWorkOrders fired and reset the variable as it should via swift request. Then my webapp loads a new page and the page refreshed, therefore cancelling my busWorkOrders reassignment. I had to change method calls in Swift. THANK YOU! I'm up voting for finding the root of my problem and being so patient. Really appreciate it @Adam – plgelso May 18 '17 at 15:07
0
  var busWorkOrders;

function jsonBusWorkOrders() {
    busWorkOrders = "New Value";
}
jsonBusWorkOrders();

alert(busWorkOrders);

try it call a function and display vaue