【问题标题】:How to trigger event only after user stopped triggering events如何仅在用户停止触发事件后触发事件
【发布时间】:2013-01-22 22:37:51
【问题描述】:

如何在用户停止使用鼠标后才触发事件?因为我不想在缩放过程中调用我的 ajax(例如,6 次放大会触发 6 个事件,但我只需要 1 个事件,当放大完成时)

这个函数会延迟执行,但是,它仍然会执行所有事件...

  google.maps.event.addListener(map, 'center_changed', function() {
    // 3 seconds after the center of the map has changed
    window.setTimeout(function() {
      myfunction());
    }, 3000);
  });

【问题讨论】:

  • 可能没有这样的事件,但是您可以清除每次鼠标移动、缩放或其他任何操作的超时,如果未清除,则用户在 3 秒内没有做任何事情,并且函数执行等.
  • 这解决了我的问题!几乎每次触发触发器时,我都会这样做:clearInterval(trigerFunctionVar); trigerFunctionVar=null; 然后将新间隔添加到trigerFunctionVar。奇迹般有效!请添加为答案,以便我选择它。

标签: javascript ajax google-maps-api-3


【解决方案1】:

您能否使用 offsetLeft 和 offsetTop 计算鼠标光标的 x 和 y 位置,然后在它们短时间内没有变化后执行您的事件?

【讨论】:

    【解决方案2】:
    var lastTriggeredTime;  
    google.maps.event.addListener(map, 'center_changed', function() {
        // 3 seconds after the center of the map has changed
        window.setTimeout(function() {
          //set last Triggered Time to now (user is still zooming)
          lastTriggeredTime = (new Date()).getTime();
          myfunction());
        }, 1000);
      });
    var myfunction = function(){
      if(lastTriggeredTime + 3000 < (new Date()).getTime()){
        //3 seconds has elapsed since last center_changed event got fired.
      }
    }
    

    【讨论】:

      【解决方案3】:

      2 个解决方案:

      var cpt = 0;
      google.maps.event.addListener(map, 'center_changed', function ()
      {
          cpt++;
          var cpt2 = cpt;
          window.setTimeout(function () {
              if (cpt == cpt2) console.log("Task is executed if it is the last event since 3sec");
          }, 3000);
      });
      

      使用事件«空闲»! http://gmaps-samples-v3.googlecode.com/svn/trunk/map_events/map_events.html

      【讨论】:

        【解决方案4】:

        我解决了这个问题:

        每次触发触发,我都这样做:clearInterval(trigerFunctionVar);trigerFunctionVar=null; 然后在trigerFunctionVar添加新的Interval

        【讨论】: