0

I have this script:

var d;
$(".circle").click(function() {
 var id = $(this).attr('id');
 var MyDiv1 = document.getElementById(id);
 var name = MyDiv1.innerHTML;
 $.get("url", function(data){
 d=data;
 });
 d=d.split(",");
 var arrayLength = d.length;
 for (var i = 0; i < arrayLength; i++) {
 if(i==id)
 custom_alert(d[i], name);
 }
});

I want to call

d = d.split(",");

Only after I'm sure d isn't undefined.
How do I execute that after the $.get functions finishes and d is assigned to the data?

asked Jun 14, 2017 at 12:47
5
  • 3
    Put it into the $.get callback? Commented Jun 14, 2017 at 12:48
  • @Jonasw sorry, what is a callback? I'm new to this... Commented Jun 14, 2017 at 12:49
  • 3
    Just place your code below d=data; (within the braces); Commented Jun 14, 2017 at 12:50
  • @Pugazh Wow, it worked. Didn't think it's that simple... :) Commented Jun 14, 2017 at 12:52
  • 2
    Possible duplicate of How do I return the response from an asynchronous call? Commented Jun 14, 2017 at 12:52

4 Answers 4

3

You could simply put it into the callback? :

 $.get("url", function(d){
 d=d.split(",");
 var arrayLength = d.length;
 for (var i = 0; i < arrayLength; i++) {
 if(i==id)
 custom_alert(d[i], name);
 }
 });

Or using ES7 (very new stuff) :

(async function(){
var d=await new Promise(function(r){
 $.get("url",r);
});
d=d.split(",");
...
})();
answered Jun 14, 2017 at 12:51
Sign up to request clarification or add additional context in comments.

Comments

2

You need to put the JS that depends on d being defined inside the get callback.

var d;
$(".circle").click(function() {
var id = $(this).attr('id');
var MyDiv1 = document.getElementById(id);
var name = MyDiv1.innerHTML;
$.get("url", function(data){
d=data;
 d=d.split(",");
 var arrayLength = d.length;
 for (var i = 0; i < arrayLength; i++) {
 if(i==id)
 custom_alert(d[i], name);
 }
 });
});
answered Jun 14, 2017 at 12:52

Comments

2

jQuery.get() is asynchronous because it is a shorthand for jQuery.ajax() who's default option for requests is async: true.

Read here about asynchronous requests.

// declare it here if you need its values later (to be a global variable), or move it inside function(){ } declared inside $.get() call.
var d;
$(".circle").click(function() {
 var id = $(this).attr('id');
 var MyDiv1 = document.getElementById(id);
 var name = MyDiv1.innerHTML;
 $.get("url", function(data){
 d=data;
 // $.get has an asynchronous answer (you don't know when it will come or if it comes). Call d.split here
 d=d.split(",");
 var arrayLength = d.length;
 for (var i = 0; i < arrayLength; i++) {
 if(i==id)
 custom_alert(d[i], name);
 }
 }); 
 });
answered Jun 14, 2017 at 12:52

Comments

1

You can only rely on d to have a value in the block where it is assigned a value, which is inside the callback function(data){...}.

var d;
$(".circle").click(function() {
 var id = $(this).attr('id');
 var MyDiv1 = document.getElementById(id);
 var name = MyDiv1.innerHTML;
 $.get("url", function(data){
 d=data.split(",");
 var arrayLength = d.length;
 for (var i = 0; i < arrayLength; i++) {
 if (i==id)
 custom_alert(d[i], name);
 }
 });
});
answered Jun 14, 2017 at 12:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.