(engine: VisualNovelEngine, enable: boolean)
| 250 | * or disabled (false); |
| 251 | */ |
| 252 | export function autoPlay (engine: VisualNovelEngine, enable: boolean): void { |
| 253 | if (enable === true) { |
| 254 | // The interval for autoplay speed is measured in seconds |
| 255 | const interval = (engine.preference ('AutoPlaySpeed') as number) * 1000; |
| 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); |
| 277 | |
| 278 | engine.element ().find ('[data-component="quick-menu"] [data-action="auto-play"] [data-string]').text (engine.string ('Stop') || 'Stop'); |
| 279 | engine.element ().find ('[data-component="quick-menu"] [data-action="auto-play"] [data-icon]').replaceWith ('<span class="fas fa-stop-circle"></span>'); |
| 280 | } else { |
| 281 | clearTimeout (engine.global ('_auto_play_timer') as ReturnType<typeof setTimeout>); |
| 282 | engine.global ('_auto_play_timer', null); |
| 283 | engine.element ().find ('[data-component="quick-menu"] [data-action="auto-play"] [data-string]').text (engine.string ('AutoPlay') || 'AutoPlay'); |
| 284 | engine.element ().find ('[data-component="quick-menu"] [data-action="auto-play"] [data-icon]').replaceWith ('<span class="fas fa-play-circle"></span>'); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * @static distractionFree - Enable or disable the distraction free mode |
no test coverage detected