admin管理员组文章数量:1327301
For example
function getResult(field) {
$.ajaxSetup ({cache: false, async: false});
$.get("api.php?field="+field, function(i) {
result = i;
});
return result;
};
The problem with this is that result bees global. If I do var result = i;
then the parent function (getResult) cannot see the variable result
.
Is there a clever way to do this?
The code that I have posted works correctly. I have set my AJAX calls to be done synchronously.
For example
function getResult(field) {
$.ajaxSetup ({cache: false, async: false});
$.get("api.php?field="+field, function(i) {
result = i;
});
return result;
};
The problem with this is that result bees global. If I do var result = i;
then the parent function (getResult) cannot see the variable result
.
Is there a clever way to do this?
The code that I have posted works correctly. I have set my AJAX calls to be done synchronously.
Share Improve this question edited Nov 29, 2011 at 14:27 joedborg asked Nov 29, 2011 at 14:07 joedborgjoedborg 18.4k33 gold badges87 silver badges122 bronze badges 10- 5 Please look on stackoverflow for this answer. It's asked at least once a day. – locrizak Commented Nov 29, 2011 at 14:08
- possible duplicate of AJAX- response data not saved to global scope? – locrizak Commented Nov 29, 2011 at 14:10
- 1 Just declare var result = ""; the first thing you do within getResult. – Christofer Eliasson Commented Nov 29, 2011 at 14:11
-
2
@ChristoferEliasson That would return
undefined
since the Ajax-request is asynchronous... – Šime Vidas Commented Nov 29, 2011 at 14:12 - @ŠimeVidas Very true, wasn't thinking clearly, sry about that. – Christofer Eliasson Commented Nov 29, 2011 at 14:14
4 Answers
Reset to default 6function doIt(arr) {
var result = [];
$.each(arr, function (y_u_key_jQuery_y_u_no_fix_api, value) {
result.push(value);
});
return result;
}
Generally what you want to do is create a local variable in the outer function that the inner function manipulates through closure scope
Let's assume that your AJAX call is synchronous. You can try this:
function getResult(field) {
var result;
$.get("api.php?field="+field, function(i) {
result = i;
});
return result;
};
This way, the variable result
is no more global.
$.get()
is an asynchronous call. Trying to return something from it will not work like you think it will. If you want to do something with the result, you should do it in the success callback.
Here is the documentation on get.
You can't actually return a value from a asynchronous call, rather you have to depend upon the response will be be handled by a callback function to be called on response arrival.
function getResult(field, callback) {
$.get("api.php?field=" + field, callback);
};
USAGE
getResult("someFieldName", function(data) {
var result = data;
// do something with the response from server
});
本文标签: jqueryBetter way to pass variable from child to parent function javascriptStack Overflow
版权声明:本文标题:jquery - Better way to pass variable from child to parent function javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742203894a2432458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论