【发布时间】:2023-01-12 22:46:32
【问题描述】:
您好,我正在努力为 React js 中的 settimeout 函数设置动态时间。 我有一长串时间和消息的键值oair。我想在特定时间显示每条消息并循环遍历整个列表。 这是我正在尝试的,但没有工作。
'''
const [timer, setTimer] = useState(0)
const [time, setTime] = useState(5000)// this is default value to start which need to update with str time value
const str=[{name:"rammy", time:1000},
{name:"james", time:4000},
{name:"crown", time:2000}]
useEffect(()=>{
const getTime= str[timer].time
setTime(getTime)
},[timer])
//when timer change it should update update time state which will be used to update time for time settime out
function increment() {
useEffect(()=>{
setTimeout(() => {
setTimer((ele)=>ele+1)
}, time);
},[timer])
} // above code is for increment time state on each iteration
function ButtonHandle(){
//setRealString(itr)
increment()
} //button handler for start timer
'''
【问题讨论】:
-
你的意思是你想通过自己的延迟时间来延迟显示的通知?
-
“不工作”没有帮助。你看到什么行为?
标签: javascript reactjs settimeout setinterval