useInterval function
- VoidCallback callback,
- Duration duration
Periodically trigger a callback when duration elapses Callback can be changed during the lifecycle, which it refresh the timer Make sure that it's memoized correctly
useInterval(() => print('elapsed'), const Duration(seconds: 1));
Implementation
void useInterval(VoidCallback callback, Duration duration) {
useEffect(() {
final timer = Timer.periodic(duration, (_) {
callback();
});
return () => timer.cancel();
}, [callback]);
}