I need to refresh this function every 5 seconds and I am calling this function inside a map function.
I tried using setIntervel function to refresh the function every 5 seconds but it wosn’t successful because when I do that the variable are not been updated dynamically.
This the function
let borderColor
const changeBorderColor = (item) => {
const today = new Date();
const currentDate = format(today, 'yyyy-MM-dd HH:mm:ss');
const redDate = format(new Date(item?.red_time || null), 'yyyy-MM-dd HH:mm:ss')
const amberDate = format(new Date(item?.amber_time || null), 'yyyy-MM-dd HH:mm:ss')
if(currentDate > redDate){
borderColor = "order-list-right order-list-left-red"
} else if (currentDate >= amberDate && currentDate < redDate){
borderColor = "order-list-right order-list-left-amber"
} else {
borderColor = "order-list-right order-list-left-green"
}
}
this is where I call the function
{orderData?.data?.map((item) => {
setInterval(() => changeBorderColor(item), 5000)
return (
<div
className={borderColor}
onClick={() => orderItems(item)}
key={item.id}
>
</div>
)
})}
I am trying to change the borderColor variable every 5 seconds using this but I am unable to achieve this.