Skip to content

Instantly share code, notes, and snippets.

@tofusoup429
Last active February 25, 2022 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tofusoup429/069a19cc32879063af75126a0082b282 to your computer and use it in GitHub Desktop.
Save tofusoup429/069a19cc32879063af75126a0082b282 to your computer and use it in GitHub Desktop.
import {useState, useEffect} from 'react'
export const useCountdown = (startFrom:number, startCountDown:boolean=false, timeoutFunction:any, stopCountingDown:boolean=false) =>{
/*
startFrom: number that the countdown starts from.
startCountDown: turnning this from false to true triggers countdown.
timeoutFunction: a function that will be triggered when countdown reaches 0
stopCountingDown: if it turns true, countdown stops
*/
const [counter, handleCounter] = useState(startFrom);
useEffect(()=>{
if(startCountDown){
const countdown = setInterval(()=>handleCounter(old=>old-1), 1000);
//there are two ways to stop countdown. One is letting it hits 0 and the other is you pass stopCountingDown true.
if(stopCountingDown){
clearInterval(countdown);
handleCounter(startFrom);
}
if(counter<0) {
clearInterval(countdown);
handleCounter(startFrom)
}
return ()=>clearInterval(countdown)
}
}, [startCountDown, stopCountingDown])
useEffect(()=>{
if(counter===0){
//when counter reaches 0, timeoutFunction is executed.
timeoutFunction();
handleCounter(startFrom)
}
}, [counter])
return {counter}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment