如何从我使用的result()回调函数中捕获返回值? [英] How can I catch the return value from the result() callback function that I'm using?
本文介绍了如何从我使用的result()回调函数中捕获返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< script type =text / javascript>
var geo = new GClientGeocoder();
function showAddress(){
var search = document.getElementById(search)。value;
// getLocations没有ret,所以wtf!
geo.getLocations(search,function(result){(result.Status.code == 200)?alert(result.Placemark [0] .Point.coordinates):alert(result.Status.code);} );
}< / script>
我需要回调的ret值,因为getLocations()不返回任何值。如何做到这一点?
解决方案
你不能。您必须以这样的方式编写代码,即需要 result
值的代码在回调中执行。
示例(
) function drawPlacemarks(marks){//用结果处理奇怪的东西
alert(marks [0] .Point.coordinates);
}
function getAddress(callback){
var search = document.getElementById(search)。value;
geo.getLocations(search,function(result){
if(result.Status.code == 200){
//将结果传递给回调
callback .Placemark);
}
});
}
getAddress(drawPlacemarks);
<script type="text/javascript">
var geo = new GClientGeocoder();
function showAddress() {
var search = document.getElementById("search").value;
// getLocations has not ret, so wtf!
geo.getLocations(search, function (result) { (result.Status.code == 200) ? alert(result.Placemark[0].Point.coordinates) : alert(result.Status.code); });
}</script>
I need the ret value of the callback as getLocations() returns no values. How can I do this?
解决方案
You can't. You have to write your code in such a way that the code that needs the result
value is executed in the callback.
Example (I just named the function in a way which seems logical to me):
function drawPlacemarks(marks) {
// do fancy stuff with the results
alert(marks[0].Point.coordinates);
}
function getAddress(callback) {
var search = document.getElementById("search").value;
geo.getLocations(search, function (result) {
if(result.Status.code == 200) {
// pass the result to the callback
callback(result.Placemark);
}
});
}
getAddress(drawPlacemarks);
这篇关于如何从我使用的result()回调函数中捕获返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文