Keep a javascript variable after an ajax call

Posted by Guillaume le Floch on Stack Overflow See other posts from Stack Overflow or by Guillaume le Floch
Published on 2012-08-29T21:32:27Z Indexed on 2012/08/29 21:38 UTC
Read the original article Hit count: 185

Filed under:
|
|

I'm new in javascript and jQuery. I'm using ajax calls to get data from my server. The fact is, I'm losing my javascript variables after the call .. Here is what I did : the variable is define outside any function and treat in an other function.

var a = 0;
function myfunction(url){
$.ajax({
        url: url,
        timeout: 20000,
        success: function(data){
                  // Do some stuff
                  // The a variable is now undefined
                },
        error: function(){
                  // Do some stuff
                }
       });
}

Everything is working fine, the only thing is that I need to keep my variables ... but it looks like it's gone ..

Does anyone know why?

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery