如何在用户停止触发事件后触发事件

时间:2013-01-22 22:37:52

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

如何在用户停止使用鼠标后触发事件?因为我不想在缩放期间调用我的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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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.
  }
}

答案 2 :(得分: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);
});

OR

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

答案 3 :(得分:-1)

我解决了这个问题:

每次触发触发器时,我执行此操作:clearInterval(trigerFunctionVar); trigerFunctionVar=null;然后将新的间隔添加到trigerFunctionVar