如何在useEffect钩子内的setInterval回调中调用两个函数

问题描述 投票:0回答:1

我在useEffect钩子中运行setInterval来连续运行两个函数,但是,只有第一个函数循环。我需要做什么才能运行第一个函数,然后运行第二个函数?

我甚至试过运行两个setInterval函数并更改它们的延迟选项来尝试和模拟我正在寻找的连续行为。但是它很小,很明显我的文字效果有问题。

   const myText = props.text;
  const textTimeout = props.textDelay;
  const funTextInterval = (textTimeout * myText.length) + 200;
  const [quickText, setQuickText] = useState([]);

  const displayFunText = (i) => {
    setTimeout(() => {
      myFunction1();
    }, textTimeout * i);
  };

  const erraseFunText = (j) => {
    setTimeout(() => {
      myFunction2();
    }, textTimeout * j);
  };

  useEffect(() => {
    const loop = () => {
      for (let i = 0; i < myText.length + 1; i += 1) {
        displayFunText(i);
      }
    };

    const reverseLoop = () => {
      for (let j = myText.length; j > 0; j -= 1) {
        erraseFunText(j);
      }
    };

    loop();
    const callLoops = () => {
      reverseLoop();
      loop();
    };

    const runLoops = useInterval(() => {
      callLoops();
    }, funTextInterval);

    return () => {
      clearInterval(runLoops);
    };
  }, []);

我希望reverseLoop()首先运行然后loop()运行,但我没有得到这种效果。

javascript reactjs loops react-hooks
1个回答
1
投票

主要问题是擦除效果的超时延迟短于显示效果的最长延迟。意识到显示和擦除效果的超时都是一次性完成的,所以如果你想要以正确的顺序执行回调(myFunction1,myFunction2),延迟应该继续增加。

以下是它的工作原理。注释表示我必须进行更正的位置:

// Extra code to define the functions/variables which you did not provide (ignore it):
const output = document.querySelector("div");
const myFunction1 = () => output.textContent = myText.slice(0, output.textContent.length+1);
const myFunction2 = () => output.textContent = myText.slice(0, output.textContent.length-1);
const props = { text: "This is a test", textDelay: 100 };
const useEffect = (cb) => cb();
const useState = () => [];
const useInterval = setInterval;
// END extra code

const myText = props.text;
const textTimeout = props.textDelay;
const funTextInterval = (textTimeout * myText.length * 2) + 200; // 2 times (show+hide)!
const [quickText, setQuickText] = useState([]);

const displayFunText = (i) => {
    setTimeout(() => {
        myFunction1();
    }, textTimeout * i);
};

const erraseFunText = (j) => {
    setTimeout(() => {
        myFunction2();
    }, textTimeout * j);
};

useEffect(() => {
    const loop = () => {
        for (let i = 0; i < myText.length; i += 1) { // fix end-condition
            displayFunText(i);
        }
    };

    const reverseLoop = () => {
        for (let j = myText.length; j < 2*myText.length; j += 1) { // fix to produce greater values (= delays)
            erraseFunText(j);
        }
    };

    const callLoops = () => { // change order:
        loop();
        reverseLoop();
    };

    callLoops(); // instead of loop()

    const runLoops = useInterval(() => {
        callLoops();
    }, funTextInterval);

    return () => {
        clearInterval(runLoops);
    };
}, []);
<div id="output"></div>

您可能希望查看promises和async函数,这可能会使这种异步更容易使用(意见不同)。

© www.soinside.com 2019 - 2024. All rights reserved.