Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Amit Merchant
Amit Merchant

Posted on

     

Handling time intervals in JavaScript

While working on anElectron appPomolectron, I needed to handle different time intervals throughsetInterval() function of JavaScript.
I basically needed to implement three timers in my app:

  • 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 differentsetInterval(). Second, finding another way to utilize the samesetInterval() for all three timers.

Solution

So, I've tried my hands on the second approach. I can use the samesetInterval() for all three timers by assigning it to a variable like below,

varpomodoroIntervalId;functionstartTimer(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);}
Enter fullscreen modeExit fullscreen mode

And then utilize the samestartTimer() across other timers by first clearing the current time interval usingclearInterval() method passing the interval ID. In our case I've assigned it to a global variablepomodoroIntervalId. This basically clears the currently running time interval, so that it can be utilized next time another timer has been set. The code would look like below...

functionresetTimer(){clearInterval(pomodoroIntervalId);}
Enter fullscreen modeExit fullscreen mode

This gives us the benefit of using the samesetInterval() function across fidderent use cases which makes the code clean and sleek. And I think it's a really handy feature of JavasScript.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
curtisautery profile image
Curtis Autery
  • Joined

I wouldn't use the seconds timer inside of your setInterval loop like that. The setInterval function attempts to fire on time, but doesn't if load if heavy. Running some quick tests with calculating primes while using your method for a counter, the true time and the counter's assumption about how much time had fast strayed quite a bit.

Instead of decrementing a timer once per interval, why don't you store the current date as a variable when the timer is instantiated? You can do the rest with creating new date objects and subtracting them from the start time to get elapsed milliseconds.

CollapseExpand
 
amit_merchant profile image
Amit Merchant
Coder, thinker and an aspiring entrepreneur.
  • Location
    India
  • Work
    Software Engineer
  • Joined

Hey@curtis, This seems really good approach. I'll definitely try it in my app. Thanks!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Coder, thinker and an aspiring entrepreneur.
  • Location
    India
  • Work
    Software Engineer
  • Joined

More fromAmit Merchant

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp