所以我在javascript中有一个更新函数,它从文件中获取json变量。我在其他函数中使用变量,我不得不为该函数调用相同的文件。我想将这些变量设置为全局,因此我不必在另一个函数中调用该文件,这是更新函数:
function XML() {
$.ajax({
type: "GET",
url: "xmlconnect.php",
dataType: "json",
success: function(data) {
for (var i in data) {
var data = data[i];
var level = data[4];
var cash = data[6];
var income = data[7];
var upkeep = data[42];
var total_income = income - upkeep;
var eincome = data[46];
var health = data[10];
var max_health = data[11];
var energy = data[12];
var max_energy = data[13];
var stamina = data[14];
var max_stamina = data[15];
var Exp = data[8];
var max_exp = data[9];
var attack = data[16];
var defense = data[17];
var skill_points = data[24];
var health_width = health / max_health * 100;
var energy_width = energy / max_energy * 100;
var stamina_width = stamina / max_stamina * 100;
var exp_width = Exp / max_exp * 100;
var sess_id = data['sess'];
if(cash < 1000) {
var user_cash = number_format(cash);
}
else {
var user_cash = format(cash);
}
$('#Cash').html('$'+format(user_cash));
$('#Income').html('$'+number_format(total_income));
$('#EIncome').html(number_format(eincome));
$('#LevelText').html(level);
$('#HealthText').html(format(health)+'/'+format(max_health));
$('#EnergyText').html(format(energy)+'/'+format(max_energy));
$('#StaminaText').html(format(stamina)+'/'+format(max_stamina));
$('#ExpText').html(number_format(exp_width)+'%');
$('#HealthWidth').css('width',''+health_width+'%');
$('#EnergyWidth').css('width',''+energy_width+'%');
$('#StaminaWidth').css('width',''+stamina_width+'%');
$('#ExpWidth').css('width',''+exp_width+'%');
$('#Update').load('activity.php');
}
}
});
}
例如,我需要在另一个名为“Increase”的函数中使用其中的一些变量而且我设置如下:
function Increase(Att) {
$.ajax({
type: "GET",
url: "xmlconnect.php",
dataType: "json",
success: function(data) {
for (var i in data) {
var data = data[i];
var max_health = data[11];
var max_energy = data[13];
var max_stamina = data[15];
var attack = data[16];
var defense = data[17];
var skill_points = data[24];
$('#AttResults').load('increase.php?att='+Att);
$('#Skills').html(number_format(skill_points));
$('#Att_attack').html(number_format(attack));
$('#Att_defense').html(number_format(defense));
$('#Att_max_health').html(number_format(max_health));
$('#Att_max_energy').html(number_format(max_energy));
$('#Att_max_stamina').html(number_format(max_stamina));
XML();
}
}
});
}
但我想让第一个XML函数成为全局变量。
答案 0 :(得分:1)