简体   繁体   English

谷歌地图动画不适用于 setInterval 和 setTimeout

[英]Google maps animation not working with setInterval and setTimeout

I'm developing animation with google maps API.我正在使用谷歌地图 API 开发动画。 I get some problem with setInteval and setTimeout.我遇到了 setInteval 和 setTimeout 的一些问题。

Actually, In line 247 in my jsfiddle I define myFunction(see below) which calls the function fi() each 300ms to animate the marker.实际上,在我的jsfiddle 的第 247 行中,我定义了 myFunction(见下文),它每 300 毫秒调用一次函数 fi() 来为标记设置动画。

 var myFunction = function(){ counter= 300; clearInterval(myInterval); var myInterval = setInterval(fi, counter, map, marker, g); }

I want that function fi() (see below) animates the marker then when it arrives to a marker it stops a while before to move.我希望该函数 fi()(见下文)为标记设置动画,然后当它到达标记时它会在移动之前停止一段时间。 To achieve this, I clear the setInterval ( Line 301 ) then I recall myFunction() to restart the animation after a delay using setTimeout.为此,我清除了 setInterval(第 301 行),然后调用 myFunction() 以使用 setTimeout 在延迟后重新启动动画。

 function fi(m, mar, l) { if (l[i].waypoint == 0) { coords= l[i]; var latlng = new google.maps.LatLng(parseFloat(coords.lat), parseFloat(coords.lng)); route.getPath().push(latlng); moveMarker(map, marker, latlng); i++; coords= l[i]; var latlng = new google.maps.LatLng(parseFloat(coords.lat), parseFloat(coords.lng)); route.getPath().push(latlng); moveMarker(map, marker, latlng); } else { clearInterval(myInterval); i++; setTimeout(myFunction, 500); } }

I'm expecting that the animation continue after some delay but it stops.我期待动画在一些延迟后继续,但它停止了。

Any suggestion ?有什么建议吗?

Thanks谢谢

Make myInterval global, and it works for me.使myInterval全球性的,它对我myInterval

updated fiddle更新的小提琴

var myInterval;
var myFunction = function(){
  counter= 300;
  clearInterval(myInterval);
  myInterval = setInterval(fi, counter, map, marker, g);
}

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

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