JS中一个函数里的变量值,怎么可以直接在另外一个函数里用引用
下面是函数中,通过函数计算得到gwp的值并判断绘图。后面我想把这个计算得到的gwp的值直接在其他函数中调用,应该怎么改比如gwp改成全局变量,麻烦指导下,有代码就更好$(...
下面是函数中,通过函数计算得到gwp的值并判断绘图。
后面我想把这个计算得到的gwp的值直接在其他函数中调用,应该怎么改
比如gwp改成全局变量,麻烦指导下,有代码就更好
$(function(){
var dom = document.getElementById("box4");
var myChart = echarts.init(dom);
var app = {};
option = null;
var y_f = [];
//全局变量,wgid在gognweizongtu.js中获取
var gwp = 0;
var xishu = [0.4,0.2,0.3,0.1];
var pjx = ["gzp","gyp","wlp","ryp"];
var pj = {"gongzhuang":"OK", "gongyi": "OK", "wuliao": "OK", "renyuan": "OK"};
var guige = [90, 90, 80, 75,85];
var xiang = ["gongzhuang","gongyi","wuliao","renyuan"];
for (var i = 0 ; i< xiang.length ; i++) {
var lent = data[xiang[i]].y.length ;
var ind = lent - 1 ;
y_f.push(data[xiang[i]].y[ind]);
if (data[xiang[i]].y[ind] < guige[i]){
pj[xiang[i]] = "NOK";
}
document.getElementById(pjx[i]).innerHTML = pj[xiang[i]];
gwp += y_f[i]*xishu[i];
}
y_f.push(88);
if (gwp > 85.5){
document.getElementById("gwpj").innerHTML ="OK";
}
gwp = gwp.toFixed(2);
document.getElementById("gwp").innerHTML =gwp;
document.getElementById("jinri").innerHTML =gwp;
document.getElementById("zuori").innerHTML =gwp;
document.getElementById("benzhou").innerHTML =gwp;
document.getElementById("lishi").innerHTML =gwp;
document.getElementById("gwp1").innerHTML =gwp1;
/*绘图代码省略*/
};
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
}) 展开
后面我想把这个计算得到的gwp的值直接在其他函数中调用,应该怎么改
比如gwp改成全局变量,麻烦指导下,有代码就更好
$(function(){
var dom = document.getElementById("box4");
var myChart = echarts.init(dom);
var app = {};
option = null;
var y_f = [];
//全局变量,wgid在gognweizongtu.js中获取
var gwp = 0;
var xishu = [0.4,0.2,0.3,0.1];
var pjx = ["gzp","gyp","wlp","ryp"];
var pj = {"gongzhuang":"OK", "gongyi": "OK", "wuliao": "OK", "renyuan": "OK"};
var guige = [90, 90, 80, 75,85];
var xiang = ["gongzhuang","gongyi","wuliao","renyuan"];
for (var i = 0 ; i< xiang.length ; i++) {
var lent = data[xiang[i]].y.length ;
var ind = lent - 1 ;
y_f.push(data[xiang[i]].y[ind]);
if (data[xiang[i]].y[ind] < guige[i]){
pj[xiang[i]] = "NOK";
}
document.getElementById(pjx[i]).innerHTML = pj[xiang[i]];
gwp += y_f[i]*xishu[i];
}
y_f.push(88);
if (gwp > 85.5){
document.getElementById("gwpj").innerHTML ="OK";
}
gwp = gwp.toFixed(2);
document.getElementById("gwp").innerHTML =gwp;
document.getElementById("jinri").innerHTML =gwp;
document.getElementById("zuori").innerHTML =gwp;
document.getElementById("benzhou").innerHTML =gwp;
document.getElementById("lishi").innerHTML =gwp;
document.getElementById("gwp1").innerHTML =gwp1;
/*绘图代码省略*/
};
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
}) 展开
展开全部
let gwpFun = ()=> {
var dom = document.getElementById("box4");
var myChart = echarts.init(dom);
var app = {};
option = null;
var y_f = [];
//全局变量,wgid在gognweizongtu.js中获取
var gwp = 0;
var xishu = [0.4, 0.2, 0.3, 0.1];
var pjx = ["gzp", "gyp", "wlp", "ryp"];
var pj = { "gongzhuang": "OK", "gongyi": "OK", "wuliao": "OK", "renyuan": "OK" };
var guige = [90, 90, 80, 75, 85];
var xiang = ["gongzhuang", "gongyi", "wuliao", "renyuan"];
for (var i = 0; i < xiang.length; i++) {
var lent = data[xiang[i]].y.length;
var ind = lent - 1;
y_f.push(data[xiang[i]].y[ind]);
if (data[xiang[i]].y[ind] < guige[i]) {
pj[xiang[i]] = "NOK";
}
document.getElementById(pjx[i]).innerHTML = pj[xiang[i]];
gwp += y_f[i] * xishu[i];
}
y_f.push(88);
if (gwp > 85.5) {
document.getElementById("gwpj").innerHTML = "OK";
}
gwp = gwp.toFixed(2);
document.getElementById("gwp").innerHTML = gwp;
document.getElementById("jinri").innerHTML = gwp;
document.getElementById("zuori").innerHTML = gwp;
document.getElementById("benzhou").innerHTML = gwp;
document.getElementById("lishi").innerHTML = gwp;
document.getElementById("gwp1").innerHTML = gwp1;
/*绘图代码省略*/
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
return gwp;
}
gwpFun()
请采纳
展开全部
定义到公共函数的任意位置都可以
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把变量的值保存在页面的一个隐藏的text中,在另一个函数里读取这个text的值。试试这个方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
函数库中的变量一般都是封装好的 , 不会对外暴露, 但是可以写个get方法吧这个参数值返回出来就可以了。 一遍都是这样处理的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把这句话 var gwp = 0;移到$(function(){上面
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询