Titanium 和 Javascript 匿名函数作用域
我试图在钛中调用 forwardGeocoder
函数,但在检索结果时遇到一些问题,请考虑以下代码:-
x = 0;
Ti.Geolocation.forwardGeocoder(startTextFieldContents, function(evt) {
var startPin = Ti.Map.createAnnotation({
longitude : evt.longitude,
latitude : evt.latitude,
pincolor : Ti.Map.ANNOTATION_GREEN
});
var startPinLocation = {
longitude : evt.longitude,
latitude : evt.latitude
}
mapview.addAnnotation(startPin);
x = 1;
});
Ti.API.log('X = ' + x);
如果您看一下 X,当我将其注销时,它是始终等于 0,即使我在匿名函数内将其设置为 1,并且因为这是一个以匿名函数作为参数的函数调用,所以我正在努力弄清楚如何检索该值并使其在forwardGeocoder 外部可用。
为了解决这个问题,根据这篇文章中的内容,我使用了以下代码:
var completed = 0;
Ti.Geolocation.forwardGeocoder(startTextFieldContents, function(evt) {
var startPin = Ti.Map.createAnnotation({
longitude : evt.longitude,
latitude : evt.latitude,
pincolor : Ti.Map.ANNOTATION_GREEN
});
startPinLocation = {
longitude : evt.longitude,
latitude : evt.latitude
}
mapview.addAnnotation(startPin);
++completed;
if (completed === 1) {
Ti.Geolocation.forwardGeocoder(finishTextFieldContents, function(evt) {
var finishPin = Ti.Map.createAnnotation({
longitude : evt.longitude,
latitude : evt.latitude,
pincolor : Ti.Map.ANNOTATION_RED
});
finishPinLocation = {
longitude : evt.longitude,
latitude : evt.latitude
}
mapview.addAnnotation(finishPin);
mapview.addRoute({
name : 'Route',
points : [startPinLocation, finishPinLocation],
color : 'green',
width : 1
});
});
}
});
I am trying to call the forwardGeocoder
function in titanium, but I am having some issues with retrieving the results, consider the following code :-
x = 0;
Ti.Geolocation.forwardGeocoder(startTextFieldContents, function(evt) {
var startPin = Ti.Map.createAnnotation({
longitude : evt.longitude,
latitude : evt.latitude,
pincolor : Ti.Map.ANNOTATION_GREEN
});
var startPinLocation = {
longitude : evt.longitude,
latitude : evt.latitude
}
mapview.addAnnotation(startPin);
x = 1;
});
Ti.API.log('X = ' + x);
If you take a look at X, when I log this out it is ALWAYS equal to 0 even though I am setting it inside my anonymous function as 1, and because this is a function call with an anonymous function as an argument I am struggling to figure out how to retrieve that value and make it available outside the forwardGeocoder.
To resolve this, based on what was said in this post I used the following code:
var completed = 0;
Ti.Geolocation.forwardGeocoder(startTextFieldContents, function(evt) {
var startPin = Ti.Map.createAnnotation({
longitude : evt.longitude,
latitude : evt.latitude,
pincolor : Ti.Map.ANNOTATION_GREEN
});
startPinLocation = {
longitude : evt.longitude,
latitude : evt.latitude
}
mapview.addAnnotation(startPin);
++completed;
if (completed === 1) {
Ti.Geolocation.forwardGeocoder(finishTextFieldContents, function(evt) {
var finishPin = Ti.Map.createAnnotation({
longitude : evt.longitude,
latitude : evt.latitude,
pincolor : Ti.Map.ANNOTATION_RED
});
finishPinLocation = {
longitude : evt.longitude,
latitude : evt.latitude
}
mapview.addAnnotation(finishPin);
mapview.addRoute({
name : 'Route',
points : [startPinLocation, finishPinLocation],
color : 'green',
width : 1
});
});
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以按照 Juhana 的建议来嵌套调用。问题是这很慢。您等待来自服务器的往返,然后才开始下一个往返,并且只有在 2 个同步(即非并行)往返之后,您才能执行所需的任务。如果数据相互依赖,则这是必要的,尽管听起来似乎并非
如此,您可以执行以下操作:
您知道这是如何异步工作的吗?有一个小问题,您无法区分这些值,因为它们的顺序无法保证,但您可以轻松更改
handle
(例如柯里化、绑定或包装)来解决该问题。您可以使用其他方法,尽管它们都基于相同的想法 - 计算已完成的任务,直到我保证所有任务都完成,然后执行我们想做的任务......
You can do as Juhana suggested to nest the calls. The problem is that that is slow. You wait for a round-trip from the server and only then begin the next round trip, and only after 2 synchronous (i.e. non-parallel) round-trips do you perform the required task. It would be necessary if the data were dependent on each other, although it sounds like they aren't
As such, you can do as follows:
Do you see how that will work asynchronously? There is a small problem that you can't distinguish between the values because their order isn't guaranteed, but you can easily change
handle
(currying, binding or wrapping for example) to fix that.You can use other approaches, although they are all based on the same idea - count completed tasks until I guarantee all are done, and then perform the task we wanted to do...