无法在我的React Elect Electon屏幕跟踪器应用程序中停止递归settimeout函数

发布于 2025-01-21 11:47:23 字数 1283 浏览 0 评论 0原文

我正在使用屏幕跟踪器应用程序,该应用程序每10分钟进行一次屏幕截图,然后将其上传到Firebase存储中。单击“开始功能”时,我正在调用启动功能,并希望当我单击“停止”按钮时停止捕获屏幕,但它不起作用,我不明白为什么。我是一个自学成才的开发人员,经验不到3个月。请帮忙。

function App() {
  // store the screenshots in an array
  const [newScreenshots, setNewScreenshots] = useState("");
  // const [screenshots, setScreenshots] = useState([]);
  const [timeoutId, setTimeoutId] = useState(false);
  const [progress, setProgress] = useState(0);

  const uploadFiles = (img) => {
    // logic
    const storageRef = ref(storage, `/screenshots/${uuidv4()}`);

    uploadString(storageRef, img, "data_url").then((snapshot) => {
      console.log("Uploaded a data_url string!");
    });
  };

  let newTimeoutId;
  const screenshot = async () => {
    setTimeoutId(true);

    const newTimeoutId = setTimeout(async () => {
      const screenshotData = await ipcRenderer.invoke("capture");
      const string = new Buffer.from(screenshotData).toString("base64");
      const img = `data:image/png;base64,${string}`;
      console.log(img);
      uploadFiles(img);
      screenshot();
    }, 600000);
  };

// This function is not working. I'm trying to call it using  button click.

  const stopScreenshot = () => {
    setTimeoutId(false);
    clearTimeout(newTimeoutId);
  };

I am working on a screen tracker app which takes screen screenshots every 10 minutes and upload it to a firebase storage. I'm calling the start function when clicking on the start function and want it to stop capturing screen when I click on the stop button, but it's not working and I can't understand why. I am a self taught developer with less than 3 months of experience. Please help.

function App() {
  // store the screenshots in an array
  const [newScreenshots, setNewScreenshots] = useState("");
  // const [screenshots, setScreenshots] = useState([]);
  const [timeoutId, setTimeoutId] = useState(false);
  const [progress, setProgress] = useState(0);

  const uploadFiles = (img) => {
    // logic
    const storageRef = ref(storage, `/screenshots/${uuidv4()}`);

    uploadString(storageRef, img, "data_url").then((snapshot) => {
      console.log("Uploaded a data_url string!");
    });
  };

  let newTimeoutId;
  const screenshot = async () => {
    setTimeoutId(true);

    const newTimeoutId = setTimeout(async () => {
      const screenshotData = await ipcRenderer.invoke("capture");
      const string = new Buffer.from(screenshotData).toString("base64");
      const img = `data:image/png;base64,${string}`;
      console.log(img);
      uploadFiles(img);
      screenshot();
    }, 600000);
  };

// This function is not working. I'm trying to call it using  button click.

  const stopScreenshot = () => {
    setTimeoutId(false);
    clearTimeout(newTimeoutId);
  };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2025-01-28 11:47:23

因此,我将避免使用settimeout,而是使用setInterval,它需要一个功能和时间间隔来启动它。
此外,我会创建一个函数takecreenshot,它只是照顾屏幕截图(也许您需要有一天按需进行屏幕截图,完成一半的工作将是很好的::))

const takeScreenshot = async () => {
const screenshotData = await ipcRenderer.invoke("capture");
      const string = new Buffer.from(screenshotData).toString("base64");
      const img = `data:image/png;base64,${string}`;

      uploadFiles(img);
      screenshot();
}

const startRecording = () => {
      setInterval(() => takeScreenshot() , 60000)
}

stopRecording = () => {clearInterval(startRecording)}

**您可能需要编辑它一点

So, I'd avoid using setTimeout and instead use setInterval that takes a function and an interval of time to fire it.
Additionally I would create a function takeScreenshot, that just takes care of taking the screenshot(maybe you'll want to take a screenshot on demand someday, having half of the job done would be nice :))

const takeScreenshot = async () => {
const screenshotData = await ipcRenderer.invoke("capture");
      const string = new Buffer.from(screenshotData).toString("base64");
      const img = `data:image/png;base64,${string}`;

      uploadFiles(img);
      screenshot();
}

const startRecording = () => {
      setInterval(() => takeScreenshot() , 60000)
}

stopRecording = () => {clearInterval(startRecording)}

**you might need to edit it a bit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文