0

如何仅在用户停止使用鼠标后触发事件?因为我不想在缩放期间调用我的 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);
  });
4

4 回答 4

0

您可以使用 offsetLeft 和 offsetTop 来计算鼠标光标的 x 和 y 位置,然后在它们在短时间内没有更改后执行您的事件吗?

于 2013-01-22T22:42:23.820 回答
0
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.
  }
}
于 2013-01-22T22:56:01.917 回答
0

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

于 2014-04-27T08:11:48.420 回答
-1

我解决了这个问题:

每次触发触发器时,我都会这样做:clearInterval(trigerFunctionVar); trigerFunctionVar=null;然后将新的间隔添加到trigerFunctionVar

于 2013-01-28T20:47:31.910 回答