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.

746 B
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import { useEffect, useState } from "react";
import { type Milliseconds, Time } from "@/utils/time";

/**
 * Returns the elapsed time since mount, in milliseconds.
 */
export function useElapsedTime(initialStartTimeMs: Milliseconds) {
  const [startTime] = useState(initialStartTimeMs);
  const [endTime, setEndTime] = useState(initialStartTimeMs);

  useEffect(() => {
    const step = 17; // 60 FPS
    const interval = setInterval(() => {
      // Need to use Date.now() here because
      // setInterval could be paused if the tab is inactive.
      setEndTime(Time.now().toMilliseconds());
    }, step);

    return () => clearInterval(interval);
  }, []);

  return endTime - startTime;
}