Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: more accurate elapsed time calculation (#361)

Myles Scolnick50905b81af63
diff --git a/frontend/src/hooks/useElapsedTime.ts b/frontend/src/hooks/useElapsedTime.ts
index bf485bd3dc..7ffa9fbf97 100644
--- a/frontend/src/hooks/useElapsedTime.ts
+++ b/frontend/src/hooks/useElapsedTime.ts
@@ -1,20 +1,23 @@
/* Copyright 2023 Marimo. All rights reserved. */
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
/**
- * Returns the elapsed time since mount.
+ * Returns the elapsed time since mount, in milliseconds.
*/
export function useElapsedTime() {
- const [time, setTime] = useState(0);
+ const startTime = useRef(Date.now());
+ const [endTime, setEndTime] = useState(Date.now());
useEffect(() => {
const step = 17; // 60 FPS
const interval = setInterval(() => {
- setTime((time) => time + step);
+ // Need to use Date.now() here because
+ // setInterval could be paused if the tab is inactive.
+ setEndTime(Date.now());
}, step);
return () => clearInterval(interval);
});
- return time;
+ return endTime - startTime.current;
}