我遇到以下问题:在JavaScript/jQuery中超时函数
我在我的网站上使用Google地图。我已将以下eventListener附加到地图本身:
google.maps.event.addListener(map, 'bounds_changed', scheduleDelayedCallback);
每次有人拖动地图时都会调用bounds_changed事件。我的问题是,在拖动过程中会多次调用它。现在我需要找到一种方法来调用回调函数,如果它没有在最后一次调用,比方说750毫秒。
我这样做是使用这两个功能:
function fireIfLastEvent() {
var now = new Date().getTime();
if (lastEvent.getTime() + 750 <= now) {
this_function_needs_to_be_delayed();
} else {
$("#main").html('Lade...');
}
}
function scheduleDelayedCallback() {
lastEvent = new Date();
setTimeout(fireIfLastEvent, 750);
}
此方法适用于Chrome和Opera很大。在IE中它有时候起作用,在Firefox中它永远不会起作用(即使750毫秒没有通过,它也会调用这些函数)。
是否有坚定的方法来超时函数调用?
谢谢。
什么是'this_function_needs_to_be_delayed'? – SLaks 2010-10-27 15:29:18
这是应该在bounds_changed事件上调用的实际函数。我把其他两个函数放在两者之间来创建这个伪超时。 - google.maps.event.addListener(map,'bounds_changed',this_function_needs_to_be_delayed); – user276289 2010-10-27 15:52:59