From d9bd2eb4924a8e49fae80f30e19a65d3b3d7ca9c Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 20 Jan 2013 21:27:49 +0000 Subject: [PATCH] This should be a fully functional timers module [#160] --- modules/timers/timers.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/timers/timers.js b/modules/timers/timers.js index d231dc1..d5676ee 100644 --- a/modules/timers/timers.js +++ b/modules/timers/timers.js @@ -6,30 +6,36 @@ var _ = require('underscore')._; var timers = function(dbot) { this.timers = dbot.db.timers; - this.runningTimers = []; + this.runningTimeouts = []; + this.runningIntervals = []; this.api = { 'addTimer': function(callback, timeout, firstDate) { var now = new Date().getTime(); if(firstDate) { console.log('Setting first timer to run at ' + firstDate); - timeout = firstDate.getTime() - now; + firstTimeout = firstDate.getTime() - now; setTimeout(function(callback) { console.log('Running first timer at ' + new Date().toUTCString()); + this.runningIntervals.push(this.api.addTimer(callback, timeout)); callback(); - this.api.addTimer(callback, timeout); - }.bind(this), timeout); + }.bind(this), firstTimeout); } else { - setInterval(function(callback) { + this.runningIntervals.push(setInterval(function(callback) { console.log('Running subsequent timer at ' + new Date().toUTCString()); callback(); - }.bind(this), timeout); + }.bind(this), timeout)); } } }; - this.onLoad = function() { - // TODO: Persist timers + this.onDestroy = function() { + for(var i=0;i