好的,函数的返回值总是返回“undefined”,这是一个问题
这是我的代码:
//called here:
var res = comms(GENSET_SN);
//the function
function comms(genserial) {
var Result;
//Webservice called here
MyServices.getCommsState(genserial, "S1", OnComplete1);
function OnComplete1(args) {
var Res = eval("(" + args + ")");
if (Res == 1) {
Result = 1;
} else {
Result = 0;
}
}
return Result;
}
请帮忙!!
最佳答案
我猜 MyServices.getCommsState(genserial, "S1", OnComplete1);
是一个 ajax 请求?
如果是这样,Result
的值将在 OnComplete1
回调中实际设置之前返回,因此 未定义
。
您需要传入一个回调函数,其中包含将使用 Result
值的代码。
您可以更改代码来执行此操作,请注意,您将函数传递给 comms,然后在 OnComplete1
中调用它:
comms(GENSET_SN, function(res) {
// do whatever you need with res here!
});
//the function
function comms(genserial, callback) {
//Webservice called here
MyServices.getCommsState(genserial, "S1", function(args) {
var Res = eval("(" + args + ")"); // I would change this aswell, dont use eval!
callback(Res == 1 ? 1 : 0);
});
}
关于asp.net 中的 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9019943/