【发布时间】:2019-07-07 16:06:07
【问题描述】:
我有一个绑定到谷歌地图上的鼠标点击事件的函数。由于该功能的性质,处理完成可能需要一些时间(0.1 秒 - 2 秒,具体取决于连接速度)。这本身并不是什么大问题,但是如果用户点击满意,这可能会导致问题,并且以后的调用有点依赖于前一个。
让后面的调用等待前面的调用完成的最佳方法是什么?甚至是处理先前调用失败的最佳方法?
我看过以下内容:
- 使用自定义
.addEventListener(Link) - 使用等待前一个已处理的 while 循环
- 使用简单的 if 语句检查是否需要重新运行前一个语句
- 使用其他形式的回调
现在是一些上下文示例代码:
this.createPath = function(){
//if previous path segment has no length
if (pathSegment[this.index-1].getPath().length === 0){
//we need the previous path segment recreated using this same function
pathSegment[this.index-1].createPath();
//now we can retry this path segment again
this.createPath();
}
//all is well, create this path segment using Google Maps direction service
else {
child.createPathLine(pathSegment[this.index-1].getEndPoint(), this.clickCoords);
}
}
自然地,这段代码会疯狂循环并创建许多请求。
【问题讨论】:
标签: javascript google-maps listener dom-events