Index: ps/trunk/binaries/data/mods/public/simulation/components/Timer.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/Timer.js +++ ps/trunk/binaries/data/mods/public/simulation/components/Timer.js @@ -73,6 +73,27 @@ }; /** + * Updates the repeat time of a timer. + * Note that this will take only effect after the next update. + * + * @param {number} timerID - The timer to update. + * @param {number} newRepeatTime - The new repeat time to use. + */ +Timer.prototype.UpdateRepeatTime = function(timerID, newRepeatTime) +{ + let timer = this.timers.get(timerID); + if (timer) + this.timers.set(timerID, { + "entity": timer.entity, + "iid": timer.iid, + "functionName": timer.functionName, + "time": timer.time, + "repeatTime": newRepeatTime, + "data": timer.data + }); +}; + +/** * Cancels an existing timer that was created with SetTimeout/SetInterval. * @param {number} id - The timer's ID returned by either SetTimeout or SetInterval. */ Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Timer.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Timer.js +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Timer.js @@ -78,3 +78,20 @@ cmpTimer.OnUpdate({ "turnLength": 3.0 }); TS_ASSERT_UNEVAL_EQUALS(fired, [["s",2500]]); + +fired = []; +let f = cmpTimer.SetInterval(10, IID_Test, "Callback", 1000, 1000, "f"); + +cmpTimer.OnUpdate({ "turnLength": 1 }); +TS_ASSERT_UNEVAL_EQUALS(fired, [["f", 0]]); + +cmpTimer.OnUpdate({ "turnLength": 1 }); +TS_ASSERT_UNEVAL_EQUALS(fired, [["f", 0], ["f", 0]]); + +cmpTimer.UpdateRepeatTime(f, 500); +cmpTimer.OnUpdate({ "turnLength": 1.5 }); +// Interval updated at next updated, so expecting latency here. +TS_ASSERT_UNEVAL_EQUALS(fired, [["f", 0], ["f", 0], ["f", 500], ["f", 0]]); + +cmpTimer.OnUpdate({ "turnLength": 0.5 }); +TS_ASSERT_UNEVAL_EQUALS(fired, [["f", 0], ["f", 0], ["f", 500], ["f", 0], ["f", 0]]);