• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How to JS / jQuery code run sequentially ?

Bulldog13

Golden Member
I have some JS code below:

JavaScript:
var outside = 0; 

$.get("demo_test.asp", function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
    outside = data
  });

outside = outside + 1;

// more calculations

Two questions :

1. How do I get access to the data variable outside of the get function ? The variable outside is not being assigned.
2. How can I (or should I, am I thinking about this wrong ?) wait for the .get() block to finish processing before moving on to my outside = outside + 1; assignment ?

I remember vaguely this has something to do with the non-blocking nature of JS. Right now all of my calculations code is living inside the .get() block...which is fine until I need to call another .get()
 
1. You don't get access to the data variable outside. The only way to do that would be to do what you are doing by setting a variable on the outter scope to it. The variable outside is being assigned. If you were to put a log statement after it's being set to data, you would see that. However in your case there, the "outside = outside + 1" is being called before it's getting assigned to data since you are assigning it to data after the get call.

2. @Ken g6 already hit this on the head. You are actually already using them in your code and you don't even realize it. The jquery get method uses the promise interface as is.

 
Back
Top