Array that is not global?

Here’s my code (with JQuery):


// Updates out there
updates_live = [];

$(document).ready(function() {
	
	// Set up!
	setup_livepulse();
	
});

function setup_livepulse()
{	
	// Get updates currently displayed on page
	$.get('/forums/get_livepulse', function(data) {
		
		var updates = data.split("\
");
		for (var i in updates)
		{
			if (updates[i] != '')
			{
				var udata = updates[i].split('|');
				updates_live.push(udata[0]);
			}
		}
		
	});
}

Although I define “updates_live” as global, whenever I try and access it outside of function(data){}, it returns as “undefined.” However, when I access it inside function(data){}, it returns the proper values. How can I make updates_live global and accessible by other functions?

Anybody have any ideas? Any help would be appreciated!

I apologize – you’re correct. In the console it returns an empty array… not undefined. How can I push the data in the $.get closure to the updates_live global variable? Here’s the complete code:


// Updates out there
var updates_live = [];

$(document).ready(function() {
	
	// Set up!
	setup_livepulse();
	
});

function setup_livepulse()
{		
	// Get updates currently displayed on page
	$.get('/forums/get_livepulse', function(data) {
		
		var updates = data.split("\
");
		for (var i in updates)
		{
			if (updates[i] != '')
			{
				var udata = updates[i].split('|');
				updates_live.push(udata[0]);
			}
		}
		
		console.log(updates_live);
		
	});
	
	console.log(updates_live);
}

I’ve checked line-for-line the other Javascript files included on this page and there is no other code interfering (or variable names).

Thank you for all of your help! :slight_smile: You rock!

The second one should return an empty array, because it’s outside the $.get closure. I don’t know why it would give you undefined.

The only thing I can think is that there must be some other code interfering. Can you show the rest of it?

Just put some more console.logs in. Yup, udata is what it should be. And updates_live is an empty array. It becomes populated through that loop. Then it’s inaccessible outside of the $.get() construct.

Any ideas?

I can’t see why that shouldn’t work. You should try to identify where exactly you lose it, by putting console.log(updates_live); all over the place, e.g. right after the function is defined.

There are lots of ways of making a global variable:

var myGlobal = []; // only if not within a closure (i.e. a function(){} block)
myGlobal = []; // bad practice
window.myGlobal = [];
window['myGlobal'] = [];

So the way you’re doing it is not advisable. As long as you’re sure it’s not in a closure, you should use the var statement.

When you say “access it outside function()data{}”, where exactly are you doing this? Are you sure it isn’t being defined (as something undefined) elsewhere? Or being deleted?

There is probably some other code interfering.

Are you sure this bit is actually working:

if (updates[i] != '')  {
  var udata = updates[i].split('|');
  updates_live.push(udata[0]);
}

Stick a console.log in there and see what udata looks like, and if updates_live is an array.

Hi there! Thanks. I have defined updates_live as “var updates_live = ;” at the top of my code now.

If I put two console.log()s here:


function setup_livepulse()
{		
	// Get updates currently displayed on page
	$.get('/forums/get_livepulse', function(data) {
		
		var updates = data.split("\
");
		for (var i in updates)
		{
			if (updates[i] != '')
			{
				var udata = updates[i].split('|');
				updates_live.push(udata[0]);
			}
		}
		
		console.log(updates_live);
		
	});
	
	console.log(updates_live);
}

The first console.log outputs updates_live correctly. The second console.log returns “undefined.” Any ideas?