简体   繁体   English

如何使Java延迟执行侦听器任务?

[英]How to make Java perform a listener task with a time delay?

Consider a Java program in which a listener triggers a certain function. 考虑一个Java程序,其中侦听器触发某个功能。 Suppose a user action activates the listener a few times in a row. 假设用户操作连续多次激活了侦听器。 My problem is: how do I make Java only execute the function only once in this triggering row and only if a certain amount of time has passed after the last activation of the listener? 我的问题是:如何使Java在此触发行中仅执行一次功能,并且仅在侦听器的最后一次激活后经过一定时间,才执行该功能?

I've tried to look up some information both on Google and on StackExchange, but I couldn't find any hints. 我试图在Google和StackExchange上查找一些信息,但是找不到任何提示。

(To provide some context: I'm trying to develop an Android app using the Google Maps API which includes drawing markers / marker clusters from a huge database of GPS points, wanting to only add the points in the current map view. If the user moves the map, the OnCameraChangeListener is activated quite a few times while the map still moves. However, since the database is quite huge and searching the GPS points for the given camera view might take some time, I don't want to trigger the function which adds the markers before the map view had been idle for, let's say, a second.) (提供一些背景信息:我正在尝试使用Google Maps API开发一个Android应用,该应用包括从庞大的GPS点数据库中绘制标记/标记簇,只想在当前地图视图中添加这些点。如果用户移动地图时,虽然地图仍在移动,但OnCameraChangeListener被激活了很多次,但是,由于数据库非常庞大,并且为给定的摄像机视图搜索GPS点可能需要一些时间,因此我不想触发该功能它会在地图视图空闲一秒钟之前添加标记。)

You can simply hold on to those two values you described in your post: 您可以简单地保留您在帖子中描述的两个值:
- Boolean isFunctionRunning -布尔值isFunctionRunning
- GregorianCalendar timeLastRun -格里高利历日历时间

Then if isFunctionRunning => false AND time since timeLastRun > 1 second, you start the function. 然后,如果isFunctionRunning => false,并且自timeLastRun> 1秒起的时间,则启动该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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