Here I have global variable userId, and i want to update it inside signInUserFunction(), to use is in other function. I have tried to define it using var, window, But all these didn't help. This variable doesn't update. As i see its about AJAX  async. So, what can i do with it?
And yes, I know that its not good to make authentication with JS, I am quite new to it. So, I am just creating random methods to improve.
var userId = 1;
function signInUser() {
  $.getJSON('http://localhost:8887/JAXRSService/webresources/generic/getAllUsers', function(data) {
  var items = [];
  var i = 0;
  $.each(data, function(firstname, value) {
    var str = JSON.stringify(value);
    data = JSON.parse(str);
    var innerId;
    for (p in data) {
      innerId = data[p].id;
      if ($('#nameSignIn').val() == data[p].first_name && $('#passwordSignIn').val() == data[p].password) { //
        userId = innerId;
        window.location.href = "content.html";
        break;
      } else {
        i++;
        if (i == data.length) {
          alert("Ощибка в логине или пароле!")
        }
       }
      }
    });
  });
}
 
     
     
    