Creating a Pomodoro Timer in JavaScript in 10 Lines of Code


Algorithm:

  1. Create a function that executes every second using setInterval.
  2. Get the total number of minutes.
  3. Convert that into seconds and store in a global variable.
  4. Decrement the seconds by β€œ1” for each second.
  5. Check if the seconds reaches 0. If true then alert the user and clear the timer.

Coding time! Readyβ†’ Set β†’ Go. Start coding 🀩

<script>
var seconds = 0;
var interval ;
function pomodoro(mins) {
   seconds = mins*60 || 0;     
   interval = setInterval(function() {

seconds--;
        if(!seconds){
             clearInterval(interval); 
             alert("🚨 It is Cool 😎. I wish you could share ");
        }
   },1000)
}
</script> 

That’s so simple to create an awesomatic 😎 super exclusive pomodoro clock. Follow Javascript JeepπŸš™Β .

If you find this helpful surprise 🎁 me here.

Share if you feel happy πŸ˜ƒ πŸ˜† πŸ™‚Β .

Leave a comment

Design a site like this with WordPress.com
Get started