Changeset

6932:f5fd2c5cdf28

util.timer: If possible, close the existing timer handle in order to have only one
author Kim Alvefur <zash@zash.se>
date Sun, 22 Nov 2015 17:25:44 +0100
parents 6931:5c2c8aeb4690
children 6935:d12917643e65
files util/timer.lua
diffstat 1 files changed, 8 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/util/timer.lua	Sun Nov 22 17:20:20 2015 +0100
+++ b/util/timer.lua	Sun Nov 22 17:25:44 2015 +0100
@@ -19,6 +19,7 @@
 
 local _add_task = server.add_task;
 
+local _server_timer;
 local _active_timers = 0;
 local h = indexedbheap.create();
 local params = {};
@@ -66,8 +67,13 @@
 	params[id] = param;
 	if next_time == nil or event_time < next_time then
 		next_time = event_time;
-		_active_timers = _active_timers + 1;
-		_add_task(next_time - current_time, _on_timer);
+		if _server_timer then
+			_server_timer:close();
+			_server_timer = nil;
+		else
+			_active_timers = _active_timers + 1;
+		end
+		_server_timer = _add_task(next_time - current_time, _on_timer);
 	end
 	return id;
 end