Index: binaries/data/mods/public/simulation/components/Timer.js =================================================================== --- binaries/data/mods/public/simulation/components/Timer.js +++ binaries/data/mods/public/simulation/components/Timer.js @@ -78,6 +78,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. */ Timer.prototype.CancelTimer = function(id) Index: binaries/data/mods/public/simulation/components/tests/test_Timer.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_Timer.js +++ binaries/data/mods/public/simulation/components/tests/test_Timer.js @@ -78,3 +78,23 @@ cmpTimer.OnUpdate({ "turnLength": 3.0 }); TS_ASSERT_UNEVAL_EQUALS(fired, [["s",2500]]); + +function testUpdateRepeatTime() +{ + 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 }); + 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]]); +}; +testUpdateRepeatTime();