I am using useInterval in order to automatically pass pictures every 5 seconds, and I would like to know if there is a way to reset the delay when the "next" button is clicked. For instance, if I click the button after 3 seconds to pass to the second picture, then the third picture is passing to the next one after the remaining 2 seconds. The behavior that I would want is if I pass to the second picture by clicking on the button after, e.g. 3 seconds, the delay will restart to 5 seconds again.
This is a summary of my code:
const currentSlide = signal(0);
const intervalTime = 5000;
function nextSlide() {
if (currentSlide.value === slideLength-1) {
return currentSlide.value = 0
}
else {
return currentSlide.value++;
}
};
useInterval(() => {
nextSlide();
}, intervalTime);
It is very similar to what I found in this post, however, Jacki didn't post the solution.
Thanks in advance.
setTick(0), but that's all that happens. What, in the code you wrote, makes you believe that it should have somehow triggered the function that you return in the deeply nested useEffect? (And please put that code in your post instead, so folks can meaningfully comment on it, after reducing it a little further to a minimal reproducible example)[timer, setTimer] = setState(-1)and aconst reset = () => { clearInterval(timer); setTimer(setInterval(..., ...)); }? Why all the effects/callbacks/etc, that just makes things needlessly complicated?