【发布时间】:2016-02-10 07:44:14
【问题描述】:
我正在使用 JavaScript。我有两个函数都计算一个值。每个函数还对服务进行 ajax 调用并获取一些数据。通常,数据只是在一个对象中返回。这两个函数我都希望在单击按钮时发生,所以我将我的两个“计算函数”都包装在另一个函数中。计算函数设置值。那些设置的值,我如何在我的包装函数中使用它们?这可能很简单,我只是没有得到
function ComputeSum() {
$.ajax({
type: 'GET',
dataType: 'json',
url: constructedURL,
success:
function(data) {
callback(data);
stopSpinner();
var TheSum = 4+4;
return TheSum;
},
error: function (xhr, status, error) {
alert("Error - Something went wrong on the retrieval of already existing Hydraulic Data.");
//alert("Unable to communicate with server.Status is: " + status + "The error is: " + error + "The xhr is: " + xhr);
stopSpinner();
}
});
}
function ComputeDIf() {$.ajax({
type: 'GET',
dataType: 'json',
url: constructedURL,
success:
function(data) {
callback(data);
stopSpinner();
var TheDif = 10-2;
return TheDif;
},
error: function (xhr, status, error) {
alert("Error - Something went wrong on the retrieval of already existing Hydraulic Data.");
//alert("Unable to communicate with server.Status is: " + status + "The error is: " + error + "The xhr is: " + xhr);
stopSpinner();
}
});
}
所以我有两个额外的基本功能。我在附加到按钮单击的另一个函数中调用这些函数
function Calculations() {
ComputeSum();
ComputeDif();
alert("The sum is: " + TheSum);
alert("The difference is: " + TheDif);
}
所以我的 ajax 调用返回一个对象,但我也希望能够使用我在包装函数内部的计算函数中创建的那些值。这可能吗?我错过了什么。在此先感谢您的帮助。
【问题讨论】:
-
这里认为不适合以一种方式发布您的问题,然后将其编辑为不同的问题。这对为您的第一个问题提供答案的人造成了伤害,但现在您已经编辑了一个完全不同的问题。如果您有一个新问题,那么您应该发布一个新问题,而不是更改旧问题。
-
现在您已将问题更改为关于返回从 ajax 调用获得的值的问题,它是重复的。
标签: javascript function return return-value