react 自动点击下一个目标
import React, {useEffect, useState, useRef} from 'react
const currentIndex = useRef(0);
// 点击事件
const handleClick = (tab: any, index: any) => {
currentIndex.current = index
}
useEffect(() => {
const timer = setInterval(() => {
handleClick(fleetData[currentIndex.current].acGroup, currentIndex.current)
// 依赖数组累计,到达数组长度后,重新设置点击第一个目标
currentIndex.current == fleetData?.length - 1? 0: currentIndex.current ++
}, 10000)
// 组件卸载时清理定时器(避免内存泄漏)
return() ={
clearInterval(timer)
}
},[fleetData]) // 依赖列表数据


浙公网安备 33010602011771号