简体   繁体   English

限制听众在设定时间内可以触发的次数?

[英]Limit the number of times a listener can fire in a set period?

Following on from a question I had yesterday: 昨天我提出一个问题:

Google Directions Service w/ Waypoints returning ZERO_RESULTS 带有航点的Google路线指示服务返回ZERO_RESULTS

Dr. Molle said that the directions_changed listener would fire again. 莫尔博士说,改变了directions_changed听众会再次开火。 They were correct. 他们是正确的。 It's firing infinitely. 它无限射击。 I'm wondering if there's a better place to put this listener, OR if there's a way to limit the amount of times it's allowed to fire in a set period of time. 我想知道是否有更好的位置放置此侦听器,或者是否有办法限制在一定时间内触发的次数。

function route(waypoint) {

distance = 5;   // Default distance

if(waypoint){

    var request = {
        origin: document.getElementById("search-input-from").value,
        destination: document.getElementById("search-input-to").value,
        waypoints: waypoint,
        optimizeWaypoints: true,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
}
else{

    var request = {
        origin: document.getElementById("search-input-from").value,
        destination: document.getElementById("search-input-to").value,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
}

var directionRendererOptions = { draggable: true };

// Make the directions request
directionService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {

        directionsDisplay.setOptions(directionRendererOptions); 
        directionsDisplay.setDirections(result);
        directionsDisplay.setMap(map);

        var path = result.routes[0].overview_path;

        // more code here that won't matter
    }
    else {
            alert("Directions query failed: " + status);
        }

     //listener for dragged route/polyline

google.maps.event.addListener(directionsDisplay, 'directions_changed', function(){

            var waypoints = directionsDisplay.getDirections().routes[0].legs[0].via_waypoints||[];

            for(var i=0;i<waypoints.length;++i){

               waypoints[i]={stopover:true,location: waypoints[i]}
            }

            route(waypoints);

        });
    });
 }

EDIT: Should explain myself better. 编辑:应该更好地解释自己。 Basically as I'm trying to redraw the route, I'm getting an infinite loop of directions_changed . 基本上,我正在尝试重新绘制的路线,我得到一个无限循环directions_changed

Also, for anyone looking at this question, I really don't think the downvote was necessary. 此外,对于任何关注此问题的人,我真的认为不必投票。 I didn't lack research or effort, the waypoints part of the documentation is terrible, and in no example did they attempt to use waypoints via LatLng objects. 我不乏研究或努力,文档的路标部分非常糟糕,在任何情况下,他们都没有尝试通过LatLng对象使用路标。 They only used locations. 他们只使用位置。

When you call route(waypoint), set a flag. 呼叫路由(航点),设置一个标志。 When the directions_handler function runs, clear the flag, don't re-render the directions. 当directions_handler函数运行时,清除标志,不要重新渲染方向。

  var directionsRedraw = false;

  google.maps.event.addListener(directionsDisplay, 'directions_changed', function(){
    if (directionsRedraw == false) {
      directionsRedraw = true;
      var waypoints = directionsDisplay.getDirections().routes[0].legs[0].via_waypoints||[];
      for(var i=0;i<waypoints.length;++i){
        waypoints[i]={stopover:true,location: waypoints[i]}
      }
      route(waypoints);
    } else { 
      directionsRedraw = false;
    }
  });

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

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