Twist of Set Interval & Scoping

RESEND OTP TIMER
let timeLeft = 30; function timer(){
timeLeft = 30;
let timerInterval;
clearInterval(timerInterval);
timerInterval = setInterval(() => {
if(timeLeft > 1) {
timeLeft--;
console.log(timeLeft);
}
},1000);
}
timer(); // call and watch
timer(); // then execute it again and
timer(); // notice the increasing speed

Let's discuss the reason behind it and the fix for the same.

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store