-2

I am having problem with the following code :

var pageviews = [
    [1, 0]
];            

$.get("http://bla..bla",
     function(res){
       pageviews.push([2, res.value]);
     }, "json"); 

I've checked the pageviews variable but the array is not updated. I can see the "res.value" on my console. So what is the problem?

BigBoss
  • 113
  • 1
  • 1
  • 5
  • 2
    When are you checking the value of `pageviews`? I am guessing that you are doing it right after the `$.get`. – Selvakumar Arumugam Apr 26 '13 at 14:07
  • 3
    If you're expecting the variable to be updated in statements immediately following the `$.get()` call, then that's your problem. The response comes back asynchronously and thus your variable will not be updated until some time later. – Pointy Apr 26 '13 at 14:07
  • 2
    http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – James Montagne Apr 26 '13 at 14:08

2 Answers2

2

Its most likely, that you check the pageviews array at the wrong spot. Liky any Ajax operation, .get() works asynchronously by default, which means you can't just check that array after the .get() call and expect the data was already updated.

Infact, its not, because it takes a while for the browser and your network to complete the data transfer. If you immediately check your array, after your .push() call, I promise the array was updated as expected.

$.get( /* ... */ );
console.log( pageviews ); // wrong result, the array was not even touched at this point

You might find jQuerys implicit promise objects helpful, you might use it like

$.get( /* ... */ ).done(function( res ) {
    pageviews.push([2, res.value]);
    console.log(pageviews);
});

That is pretty much the exact same like you already do, but its syntax is probably in a more convenient fashion. The "done()" thing should make it pretty much clear.

Of course, you can also mixup those things (for whatever reason), like

$.get("http://bla..bla", function(res){
   pageviews.push([2, res.value]);
}, "json").done(function() {
   console.log( pageviews );
});
jAndy
  • 231,737
  • 57
  • 305
  • 359
0

If you are checking pageviews straight after calling $.get, it will 99% of the time not have any change. This is because the request is asynchronous (as opposed to synchronous) meaning that it occurs in the background and lets the main thread of Javascript to keep executing.

Try console.log inside here:

$.get("http://bla..bla",
 function(res){
   pageviews.push([2, res.value]);
   console.log(pageviews); //<-- This will log the updated value to the console
 }, "json"); 
Turnerj
  • 4,258
  • 5
  • 35
  • 52
  • 100% of the time in fact. – James Montagne Apr 26 '13 at 14:20
  • While it is true that it wouldn't have the changed value, as it is asynchronous, if the main JS thread froze long enough it would get a result back before getting to that line. However unlikely that may be, it is still a possibility. This situation though is even more unlikely as there is no intensive processing that could lock up the main thread between the AJAX request and the next line of Javascript. Still possible, just extremely unlikely. – Turnerj Apr 26 '13 at 14:23