计算在使用React应用程序上实际花费的时间

huangapple go评论58阅读模式
英文:

Calculate Actual time spent on an app using React

问题

我需要创建一个功能,其中需要捕获应用程序中模块的总时间。现在我们有5个模块,每个模块都有不同的页面和流程。

假设我们有一个模块来捕获客户信息。因此,对于这个模块的总时间将是在该流程中每个页面(基本信息、地址详细信息、教育背景等)上花费的时间之和。

我们还需要计算子模块的时间。例如,地址详细信息有当前地址和永久地址。在这种情况下,我们需要知道在每个页面上花费的时间,并且总时间应分配给在地址详细信息上花费的时间。

用户还可以在半途关闭页面或返回到上一个页面,这种情况下,应该开始或继续该特定页面的时间。

想知道如何处理这个问题以及后端API应该如何设计,因为我对此感到困惑。

非常感谢提前帮助。

英文:

I have to create a functionality where I need to capture the total time spent on a module in the application. Now we have 5 modules and each module has different pages and flows.

Say, we have a module to capture customer information. So the total time spent on this module would be the sum of time spent on each page (basic information, address details, education background, etc) in this flow.

We also need to calculate the time for sub-modules. For example, address details have a current and permanent address. In that case, we need to know the time spent on each of these pages and the total time should be assigned to the time spent in address details

Users can also close the page midway or go back to the previous page, and in that case, the time for that particular page should be started or resumed.

Would like to know how can I go about this and how the backend APIs should be designed for this as I am not able to wrap my head around this.

Many thanks in advance

答案1

得分: 4

你可以使用React状态和内置的Date对象的组合。

对于每个模块或子模块中的页面,创建一个状态变量来存储在该页面上花费的时间。

const [timeSpentOnPage, setTimeSpentOnPage] = useState(0); // 毫秒

使用useEffect钩子在页面渲染后立即启动计时器。

useEffect(() => {
  const intervalId = setInterval(() => {
    setTimeSpentOnPage(prevTime => prevTime + 1000); // 每秒增加1秒
  }, 1000); // 每1秒
  return () => clearInterval(intervalId); // 清理
}, []); // 仅在挂载时运行

为了处理用户离开页面然后稍后返回的情况,您可以将用户离开页面时的时间戳存储在本地存储中。

useEffect(() => {
  const handleUnload = () => {
    localStorage.setItem('pageLeaveTime', new Date().getTime());
  };
  window.addEventListener('beforeunload', handleUnload);
  return () => window.removeEventListener('beforeunload', handleUnload); // 清理
}, []);

当用户返回页面时,您可以从本地存储中检索先前的离开时间,并从那个时间点恢复计时器。

useEffect(() => {
  const pageLeaveTime = localStorage.getItem('pageLeaveTime');
  if (pageLeaveTime) {
    const timeSpentSoFar = new Date().getTime() - Number(pageLeaveTime);
    setTimeSpentOnPage(timeSpentSoFar);
  }
}, []);

为了计算在模块或子模块上花费的总时间,您可以使用另一个状态变量来存储跨所有页面累积的总时间。

const [totalTimeSpent, setTotalTimeSpent] = useState(0); // 毫秒

每当用户完成一个页面并转到下一个页面或关闭模块时,您可以将在该页面上花费的时间添加到模块的累积时间中。

const handlePageCompletion = () => {
  setTotalTimeSpent(prevTime => prevTime + timeSpentOnPage);
  localStorage.removeItem('pageLeaveTime');
  setTimeSpentOnPage(0);
};

const handleModuleClose = () => {
  setTotalModuleTime(prevTime => prevTime + totalTimeSpent);
  setTotalTimeSpent(0);
};
英文:

You could use a combination of React state and built-in Date object.

For each page in a module or sub-module, create a state variable to store the time spent on that page

const [timeSpentOnPage, setTimeSpentOnPage] = useState(0); // in milliseconds

useEffect hook to start a timer for each page as soon as it is rendered

useEffect(() => {
  const intervalId = setInterval(() => {
    setTimeSpentOnPage(prevTime => prevTime + 1000); // increment by 1 second
  }, 1000); // every 1 second
  return () => clearInterval(intervalId); // cleanup
}, []); // run only once on mount

To handle cases where the user navigates away from the page and comes back later, you could store the timestamp when the user leaves the page in local storage

useEffect(() => {
  const handleUnload = () => {
    localStorage.setItem('pageLeaveTime', new Date().getTime());
  };
  window.addEventListener('beforeunload', handleUnload);
  return () => window.removeEventListener('beforeunload', handleUnload); // cleanup
}, []);

When the user returns to the page, you could retrieve the previous leave time from local storage and resume the timer from that point

useEffect(() => {
  const pageLeaveTime = localStorage.getItem('pageLeaveTime');
  if (pageLeaveTime) {
    const timeSpentSoFar = new Date().getTime() - Number(pageLeaveTime);
    setTimeSpentOnPage(timeSpentSoFar);
  }
}, []);

To calculate the total time spent on a module or sub-module, you could use another state variable to store the cumulative time spent across all its pages

const [totalTimeSpent, setTotalTimeSpent] = useState(0); // in milliseconds

Whenever the user completes a page and moves on to the next one or closes the module, you could add the time spent on that page to the cumulative time for the module

const handlePageCompletion = () => {
  setTotalTimeSpent(prevTime => prevTime + timeSpentOnPage);
  localStorage.removeItem('pageLeaveTime');
  setTimeSpentOnPage(0);
};

const handleModuleClose = () => {
  setTotalModuleTime(prevTime => prevTime + totalTimeSpent);
  setTotalTimeSpent(0);
};

huangapple
  • 本文由 发表于 2023年4月19日 15:54:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定