如何在用户停止触发事件后触发事件
问题描述:
如何在用户停止使用鼠标后触发事件?因为我不想在缩放期间调用我的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);
});
答
我解决了这个问题:
每次触发器被触发,我这样做:clearInterval(trigerFunctionVar);
trigerFunctionVar=null;
,然后添加新的时间间隔,以trigerFunctionVar
答
你能使用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解决方案:
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
有可能是没有这样的事件,但你可以在每个鼠标移动,缩放或任何清除超时,如果它不被清除,用户did'nt 3秒钟做任何事,和函数执行等 – adeneo
那解决了我的问题!每当触发器触发时,我都会这样做:'clearInterval(trigerFunctionVar); trigerFunctionVar = null;'然后将新的Interval添加到'trigerFunctionVar'。奇迹般有效!请添加为答案,以便我可以选择它。 – Andrew