简体   繁体   English

如何仅在directionsDisplay结束后运行功能?

[英]How to run a function only after directionsDisplay ends?

I am following up to this question , where there is an asynchronous request to draw a route from point A to B, called setDirections . 我正在回答这个问题 ,那里有一个异步请求来绘制从点A到B的路由,称为setDirections In the end, we want an endpoint to be at the center, but by default the map centers to show most of the route. 最后,我们希望端点位于中心,但默认情况下,地图中心会显示大部分路线。 Simply adding setCenter(endpoint) after setDirections does not result in centering at the endpoint. setDirections之后简单地添加setCenter(endpoint)不会导致在端点居中。

I reasoned by using a setTimeout, that setDirections seems to be another asynchronous function, because after waiting a bit, the map centers as I wish. 我通过使用setTimeout进行推断,该setDirections似乎是另一个异步函数,因为在setDirections片刻之后,地图便按我的意愿居中了。

So how can I ensure my setCenter runs only after setDirections is done? 那么,如何确保setCenter仅在setDirections完成后才能运行? I thought about callbacks but it is not doing what I want. 我想到了回调,但它没有做我想要的。

In my code I did the following, using patterns from Recurial and JavaScript Callback after calling function : 在我的代码中,我在调用function之后使用了RecurialJavaScript Callback的模式进行了以下操作:

  1. wrap the existing directions drawing code with function wrapper (callback) { 使用function wrapper (callback) {现有的路线图绘制代码function wrapper (callback) {
  2. the directions request and drawing is asynchronous, so inside the successful response block, I call callback(this.marker.getPosition()) 方向请求和绘制是异步的,因此在成功的响应块内,我调用callback(this.marker.getPosition())
  3. after defining the wrapper , I call wrapper(function(latLng) { map.setCenter(latLng) }) 定义wrapper ,我调用wrapper(function(latLng) { map.setCenter(latLng) })

Here's a JSFiddle. 这是一个JSFiddle。 http://jsfiddle.net/EeXQF/2/ http://jsfiddle.net/EeXQF/2/

The setTimeout "solution" is there, just needs to be uncommented. setTimeout“解决方案”在那里,只需要取消注释即可。

(to be honest I dont really follow what you asking, I guess there is a lot of background to your question, which I just dont have. ) (说实话,我并没有真正听从您的要求,我想您的问题有很多背景,而我只是没有。)

But, one thing to note, Javascript is generally single threaded. 但是要注意的是,Javascript通常是单线程的。 Only one bit of code is getting executed at once. 一次只执行一小段代码。

So your code asks for something to happen. 因此,您的代码要求发生某些事情。 But it can't generally happen, while your code is still running. 但是当您的代码仍在运行时,通常不会发生这种情况。 The browser will execute it when your code has finished. 代码完成后,浏览器将执行它。 Actions you 'start' will get added to a queue, and run later. 您“开始”的操作将被添加到队列中,并在以后运行。

setTimeout, adds the callback onto the end of the queue. setTimeout,将回调添加到队列的末尾。 So main function will finish. 这样主要功能将完成。 maps will get a chance to do its stuff (it has stuff in the queue), then your callback will fire. 地图将有机会执行其工作(队列中有工作),然后您的回调将触发。

(its more complicated than that in reality, but might help explain what you seeing) (它比实际情况要复杂,但可能有助于解释您看到的内容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM