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])  // 依赖列表数据

企业微信截图_859d540e-b844-494d-b090-aae5e693939c

 

posted @ 2025-11-06 08:35  小蘑菇123  阅读(1)  评论(0)    收藏  举报