SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array that is not global?

    Here's my code (with JQuery):

    Code Javascript:
    // 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("\n");
    		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?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code javascript:
    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.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code javascript:
    function setup_livepulse()
    {		
    	// Get updates currently displayed on page
    	$.get('/forums/get_livepulse', function(data) {
     
    		var updates = data.split("\n");
    		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?

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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?

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code javascript:
    // 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("\n");
    		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! You rock!

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anybody have any ideas? Any help would be appreciated!

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Are you sure this bit is actually working:

    Code javascript:
    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.

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •