call var in function to outside the function

Enkhtuvshin

I'm trying to call this variable outside the function but I get undefined as a result. I understand without var the variable could be called in global scope but i get undefined. I also tried setting the var outside the function and calling it but no success also. Here's my simplified code thank you :)

function function1() {
  $.getJSON('random.json')
    .success(successfunction)
    .error(failfunction);

  function successfunction(data) {
    testvar = (data.name);
  }
  function failfunction(error) {
    console.log(error);
  }
};

console.log(testvar);

used $( document ).ajaxStop(function() {} to load the var after ajax load.

Abdennour TOUMI

if you want global scope attach your variable to global object like window

function successfunction(data){
    window.testvar = "var";
} 

By that you will be sure that it becomes global .


However, you should note that successfunction is a callback that will be running later on : it is running after timeout & only if the ajax call succeeded .

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

var not affected outside of function?

From Dev

Call a function outside main ()

From Dev

Call a function outside main ()

From Dev

popup outside of a function call

From Dev

Change value of var outside the function

From Dev

Angular var used outside of function

From Dev

Call a function of a library, outside the library

From Dev

Call This Function Outside of Form Submit

From Dev

Call controller function from outside

From Dev

How to Call PHP function outside

From Dev

Call MainActivity function outside the class

From Dev

Bind scope to function outside of function call

From Dev

Call javascript function result outside function

From Dev

How to access var within function but outside subfunction

From Dev

Call function outside of class from UIButton

From Dev

Can I call function outside viewmodel on knockout?

From Dev

Call function outside inner object with same name?

From Dev

How to call a function outside of javascript click event

From Dev

JS OOP outside prototype function call ( scope )

From Dev

Angular JS - Call a Directive function in an outside Controller

From Dev

call function on click outside of a particular div

From Dev

Get object after outside function call

From Dev

Can I call function outside viewmodel on knockout?

From Dev

How call variable outside function .each with Jquery?

From Dev

Call function outside of current namespace in C++

From Dev

how to call specific function from outside an application

From Dev

How to call a function outside $ionicActionSheet on buttonClicked?

From Dev

AngularJS : Call controller function from outside with vm

From Dev

jQuery call function outside of defined plugin

Related Related

HotTag

Archive