Problem While working on an app , I needed to handle different time intervals through function of . I basically needed to implement three timers in my app: Electron Pomolectron setInterval() JavaScript Pomodoro of 25 minutes Short break of 5 minutes Long break of 10 minutes I could think of two ways to implement countdown timer for all three cases. One, to implement different timers by using three different . Second, finding another way to utilize the same for all three timers. setInterval() setInterval() Solution So, I’ve tried my hands on the second approach. I can use the same for all three timers by assigning it to a variable like below.. setInterval() var pomodoroIntervalId; function startTimer(duration, display) { timer = duration; pomodoroIntervalId = setInterval(function(){ if (--timer < 0) { timer = duration; } minutes = parseInt(timer/60, 10); seconds = parseInt(timer%60, 10); minutes = minutes < 10 ? '0'+minutes : minutes; seconds = seconds < 10 ? '0'+seconds : seconds; display.textContent = minutes+ ":" + seconds; if(minutes == 0 && seconds == 0){ notifyUser(); } }, 1000);} And then utilize the same across other timers by first clearing the current time interval using method passing the interval ID. In our case I’ve assigned it to a global variable . This basically clears the currently running , so that it can be utilized next time another timer has been set. The code would look like below.. startTimer() clearInterval() pomodoroIntervalId time interval function resetTimer() { clearInterval(pomodoroIntervalId);} This gives us the benefit of using the same function across fidderent use cases which makes the code clean and sleek. And I think it’s a really handy feature of JavasScript. setInterval() Happy coding! is how hackers start their afternoons. We’re a part of the family. We are now and happy to opportunities. Hacker Noon @AMI accepting submissions discuss advertising & sponsorship To learn more, , , or simply, read our about page like/message us on Facebook tweet/DM @HackerNoon. If you enjoyed this story, we recommend reading our and . Until next time, don’t take the realities of the world for granted! latest tech stories trending tech stories