我有以下功能:
function charts(a, b, c){
//get the variables of function values and do some stuff with it
}
我通过a,b,c从另一个函数start得到变量:
function start(){
var a = 1;
var b = 2;
var c = 3;
charts(a, b, c);
}
如何在功能图表中使用此功能的变量:
function values() {
$.ajax({
url: site_url + "/?key=XXX&email=" + email + "&values=yes" + '&callback=?',
type: "GET",
dataType: 'json',
success: function (data) {
value1 = data.value1;
value2 = data.value2;
value3 = data.value3;
}
});
}
答案 0 :(得分:2)
function charts(a, b, c){
//get the variables of function values and do some stuff with it
}
function values(){
$.ajax({
url: site_url + "/?key=XXX&email=" + email + "&values=yes" + '&callback=?',
type: "GET",
dataType: 'json',
success: function(data){
value1 = data.value1;
value2 = data.value2;
value3 = data.value3;
charts(value1, value2, value3);
}
});
}
修改强>
如果您想将值传递给图表,然后调用ajax请求并在回调中使用a
,b
和c
,请尝试以下操作:
function charts(a, b, c) {
return function () {
$.ajax({
url: site_url + "/?key=XXX&email=" + email + "&values=yes" + '&callback=?',
type: "GET",
dataType: 'json',
success: function (data) {
value1 = data.value1;
value2 = data.value2;
value3 = data.value3;
console.log(a);
console.log(b);
console.log(c);
}
});
}
}
用法:
var chartsFunction = charts(1,2,3);
chartsFunction();
输出:
1
2
3