首页 > 解决方案 > 在函数开始运行之前设置计时器

问题描述

const onToggle = (id) => {
    setActive(id === active ? null : id);
    scroller.scrollTo(id, {
      smooth: true,
      duration: 500,
      spy: true,
      exact: true,
      offset: -15,
    });
  };

如何在scroller.scrollTo开始运行前设置 0.5 秒延迟?我需要它在决定滚动到哪里之前等待。

标签: javascriptreactjsuser-interfacefrontend

解决方案


如果你想延迟 0.5s,你可以使用setTimeout

setTimeout(() => {
  scroller.scrollTo(id, {
    smooth: true,
    duration: 500,
    spy: true,
    exact: true,
    offset: -15,
  });
}, 500);

推荐阅读