From 7a39a16c744efc688fa905aa6e65690a9aaad70e Mon Sep 17 00:00:00 2001 From: Matt Russell Date: Tue, 1 Oct 2024 11:36:46 -0400 Subject: [PATCH] Updates --- heap.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/heap.js b/heap.js index fe6d799..7468b9a 100644 --- a/heap.js +++ b/heap.js @@ -105,6 +105,7 @@ var LINE_COLOR = BLUE; var TEXT_COLOR = DARKGREY; var ALGO_SPEED = 1; var ALGO_STATUS = 'stopped'; +var USER_PAUSED = false; // these will change dependent on the above params var BOX_DIM = DEF_BOX_DIM; @@ -119,6 +120,16 @@ var PAUSE_CONTROLLER; var SWAPS = []; var SWAP_IDX = 0; +function handleVisibilityChange() { + if (document.hidden && ALGO_STATUS === "running") { + ALGO_STATUS = "paused"; + } + if (!document.hidden && ALGO_STATUS === "paused" && !USER_PAUSED) { + ALGO_STATUS = "running"; + } +} +document.addEventListener("visibilitychange", handleVisibilityChange); + function createNode( x, y, z, i, format ) { @@ -775,14 +786,17 @@ function runFunc( pcFunc, userFunc, button ) { button.name('pause'); button.updateDisplay(); pcFunc(); + USER_PAUSED = false; }else if (ALGO_STATUS === "running") { ALGO_STATUS = "paused"; button.name('resume'); button.updateDisplay(); + USER_PAUSED = true; }else if (ALGO_STATUS === "paused") { ALGO_STATUS = "running"; button.name('pause'); button.updateDisplay(); + USER_PAUSED = false; } } else { userFunc();