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 @@ -12,7 +12,7 @@ }; /** - * Returns time since the start of the game in milliseconds. + * @returns {number} The duration since the start of the game in milliseconds. */ Timer.prototype.GetTime = function() { @@ -20,7 +20,7 @@ }; /** - * Returns the duration of the latest turn in milliseconds. + * @returns {number} The duration of the latest turn in milliseconds. */ Timer.prototype.GetLatestTurnLength = function() { @@ -28,40 +28,36 @@ }; /** - * Create a new timer, which will call the 'funcname' method with arguments (data, lateness) + * Create a new timer, which will call the 'funcname' method with arguments (data) * on the 'iid' component of the 'ent' entity, after at least 'time' milliseconds. - * 'lateness' is how late the timer is executed after the specified time (in milliseconds). - * Returns a non-zero id that can be passed to CancelTimer. + * @param {number} ent the entity the timer will be assigned to. + * @param {number} iid the component iid of the timer + * @param {string} funcname the name of the function to be called in the component. + * @param {number} time the delay before running the function. + * @param {any} data the data to pass to the function. + * @returns {number} A non-zero id that can be passed to CancelTimer. */ Timer.prototype.SetTimeout = function(ent, iid, funcname, time, data) { - let id = ++this.id; - - this.timers.set(id, { - "entity": ent, - "iid": iid, - "functionName": funcname, - "time": this.time + time, - "repeatTime": 0, - "data": data - }); - - return id; + return this.SetInterval(ent, iid, funcname, time, 0, data); }; /** - * Create a new repeating timer, which will call the 'funcname' method with arguments (data, lateness) - * on the 'iid' component of the 'ent' entity, after at least 'time' milliseconds - * and then every 'repeattime' milliseconds thereafter. - * It will run multiple times per simulation turn if necessary. - * 'repeattime' must be non-zero. - * 'lateness' is how late the timer is executed after the specified time (in milliseconds). - * Returns a non-zero id that can be passed to CancelTimer. + * Create a new timer, which will call the 'funcname' method with arguments (data) + * on the 'iid' component of the 'ent' entity, after at least 'time' milliseconds every + * 'repeattime' milliseconds. + * @param {number} ent the entity the timer will be assigned to. + * @param {number} iid the component iid of the timer + * @param {string} funcname the name of the function to be called in the component. + * @param {number} time the delay before running the function. + * @param {number} repeattime If non 0 the interval between each execution of the function. + * @param {any} data the data to pass to the function. + * @returns {number} A non-zero id that can be passed to CancelTimer. */ Timer.prototype.SetInterval = function(ent, iid, funcname, time, repeattime, data) { - if (typeof repeattime != "number" || !(repeattime > 0)) - error("Invalid repeattime to SetInterval of "+funcname); + if (typeof repeattime != "number" || repeattime < 0) + error("An invalid repeat time was passed to SetInterval of " + funcname); let id = ++this.id; @@ -79,13 +75,13 @@ /** * Cancels an existing timer that was created with SetTimeout/SetInterval. + * @param {number} id the timer's id returned by either SetTimeout or SetInterval */ Timer.prototype.CancelTimer = function(id) { this.timers.delete(id); }; - Timer.prototype.OnUpdate = function(msg) { this.turnLength = Math.round(msg.turnLength * 1000);