javascript - 在函数开始运行之前设置计时器
问题描述
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 秒延迟?我需要它在决定滚动到哪里之前等待。
解决方案
如果你想延迟 0.5s,你可以使用setTimeout
:
setTimeout(() => {
scroller.scrollTo(id, {
smooth: true,
duration: 500,
spy: true,
exact: true,
offset: -15,
});
}, 500);
推荐阅读
- android - 从 ArrayList 中删除 IntRanges 并拆分结果
- c# - 如果下拉菜单中只有 1 个条目,如何触发 Combobox Selection Changed 事件
- php - 从一个url下载的json信息是加密的
- javascript - why does it not 'add to cart' when I click add to cart?
- excel - COUNTIF 跨多个“重命名”工作表
- asp.net - 在单页应用程序中添加服务器端代码
- r - Rmarkdown YAML 标头中的作者从属关系,使用 rticles
- ruby-on-rails - 如何将 ajax 附加到 Rails 中的关注/取消关注链接
- android - ListView:如何在单击 ListView 中的按钮时转到新活动?
- c++ - Visual Studio 17 调试属性未导出到模板