简体   繁体   中英

How to trigger event only after user stopped triggering events

How to trigger event only after user stopped using his mouse? Because I don't want to call my ajax during the zoom (for example, 6 zoom-ins would trigger 6 events, but I need only 1 event, when zoom-in is done)

This function delays the execution, nevertheless, it still executes all events...

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

您能否使用offsetLeft和offsetTop来计算鼠标光标的x和y位置,然后在短时间内未更改它们之后执行您的事件?

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 solutions :

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

Use event « idle » ! http://gmaps-samples-v3.googlecode.com/svn/trunk/map_events/map_events.html

I solved this problem:

Each time trigger is triggered, I do this: clearInterval(trigerFunctionVar); trigerFunctionVar=null; And then add new Interval to trigerFunctionVar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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