| 256 | let expected = Date.now () + interval; |
| 257 | |
| 258 | const timerFn = () => { |
| 259 | const now = Date.now () - expected; // the drift (positive for overshooting) |
| 260 | if (now > interval) { |
| 261 | // something really bad happened. Maybe the browser (tab) was inactive? |
| 262 | // possibly special handling to avoid futile "catch up" run |
| 263 | } |
| 264 | engine.proceed ({ userInitiated: false, skip: false, autoPlay: true }).then (() => { |
| 265 | expected += interval; |
| 266 | setTimeout (engine.global ('_auto_play_timer') as () => void, Math.max (0, interval - now)); // take into account drift |
| 267 | }).catch (() => { |
| 268 | // An action waiting for user interaction or something else |
| 269 | // is blocking the game. |
| 270 | expected += interval; |
| 271 | setTimeout (engine.global ('_auto_play_timer') as () => void, Math.max (0, interval - now)); // take into account drift |
| 272 | }); |
| 273 | }; |
| 274 | |
| 275 | engine.global ('_auto_play_timer', timerFn); |
| 276 | setTimeout (timerFn, interval); |