Get variables after ajax finishes

Get variables after ajax finishes

I'm trying to get at variables returned from an ajax.  Found out that those variables aren't available outside the function, so I wrote those variables to data attributes & then trying to call those later.

  1. function myajaxcall() {
  2.     var v = new Array();
  3.     $.ajax({
  4.       url: "pathto/check.php",
  5.       dataType: "json"
  6.     }).done (function(check) {
  7.       v = check;
  8.       console.log('v Inside: ' , v); // Getting correct values, BUT it's running twice, for some reason?
  9.       $('#mycheck').attr({'data-uflag': v.uflagexists, 'data-cflag': v.cflagexists});
  10.     });
  11.   }
  12.  myajaxcall();
  13.   $.when(myajaxcall()).done(function() {  // USED WHEN DONE TO INSURE DATA VARIABLES ARE AVAILABLE
  14.     console.log('Done running');
  15.     var uflagcheck = $('#mycheck').attr('data-uflag');
  16.     console.log('Uflagcheck: ' + uflagcheck);  // GETTING DEFAULT VALUE OF '#' FOR DATA
  17.     if (uflagcheck == true) {
  18.       console.log('Uflagcheck is True');
  19.       ...
  20.     }
  21. });

This is the order I want things to run:
 - ajax call (done assigns the data attributes)
 - other code runs after ajax & uflagcheck picks up data value to use in later conditionals

Attached a screenshot of the v values, which are correct.

How do I get this stuff to run in proper order & pull the values correctly?