diff --git a/game.js b/game.js index 11af821..e2fbabc 100644 --- a/game.js +++ b/game.js @@ -94,33 +94,31 @@ Returns the timeout id - but you still need to cancel it through stopTimer! */ function startTimer(config, state) { - if (config.loop) { - const timeout = setTimeout(() => { - const result = config.func(state, config); - refresh(); + const timeout = setTimeout(() => { + const result = config.func(state, config); + refresh(); - // the timer may have terminated itself! - // we have to make sure it still exists + // the timer may have terminated itself! + // we have to make sure it still exists - if (state.timers.some(x => x.timeout == timeout)){ - state.timers = state.timers.filter(x => x.timeout != timeout); - if (typeof(result) === "number") { - config.delay = result; - } - - // you shouldn't use a delay of 0 anyway - if (result) { - startTimer(config, state); - } + if (state.timers.some(x => x.timeout == timeout)){ + state.timers = state.timers.filter(x => x.timeout != timeout); + if (typeof(result) === "number") { + config.delay = result; + } + // you shouldn't use a delay of 0 anyway + if (result && config.loop) { + startTimer(config, state); } - }, config.delay); + } - state.timers.push({id: config.id, timeout: timeout, room: config.room, classes: config.classes || []}); + }, config.delay); - return timeout; - } + state.timers.push({id: config.id, timeout: timeout, room: config.room, classes: config.classes || []}); + + return timeout; } function stopTimer(id, state) {