From 2439d35db6e636ae7c30a46c52851f0137204a3b Mon Sep 17 00:00:00 2001 From: mcmlxxix <> Date: Wed, 12 Dec 2012 05:03:50 +0000 Subject: [PATCH] add nextrun property (returns ms until next event execution) --- exec/load/event-timer.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/exec/load/event-timer.js b/exec/load/event-timer.js index a6bf07b502..28fc91ee21 100644 --- a/exec/load/event-timer.js +++ b/exec/load/event-timer.js @@ -26,17 +26,23 @@ } //will print "hello" 10 times at 10 second intervals - timer.addEvent(10000,10,hello); + var event1 = timer.addEvent(10000,10,hello); //will print "hello" infinitely at 20 second intervals - timer.addEvent(20000,true,print,"hello"); + var event2 = timer.addEvent(20000,true,print,"hello"); + + //will print "hello" once after 30 seconds + var event3 = timer.addEvent(30000,false, while(timer.events.length > 0) { + + // iterate events list and run any events that are scheduled timer.cycle(); mswait(1000); + + // mark event1 for deletion on next timer.cycle() + event1.abort = true; } - - //script will end when the event runs its course */ function Timer() { this.VERSION = "$Revision$".replace(/\$/g,'').split(' ')[1]; @@ -100,9 +106,14 @@ function Timer() { this.abort=false; /* run event */ this.run = function() { - this.action.apply(this.context,this.arguments); this.lastrun = Date.now(); + this.action.apply(this.context,this.arguments); } + + /* poll event for time remaining until next run */ + this.__defineGetter__("nextrun",function() { + return (this.interval - (Date.now() - this.lastrun)); + }); } }; -- GitLab