From 944c36c413d68cc4756f89af63bd03b3ae16f5a0 Mon Sep 17 00:00:00 2001 From: Tushar Kapoor <82567111+Kapoor-Tushar@users.noreply.github.com> Date: Fri, 7 Oct 2022 04:18:36 +0530 Subject: [PATCH] Create ImplementingTimers.js Created a clock timer --- Basic Programs/ImplementingTimers.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Basic Programs/ImplementingTimers.js diff --git a/Basic Programs/ImplementingTimers.js b/Basic Programs/ImplementingTimers.js new file mode 100644 index 0000000..e67556a --- /dev/null +++ b/Basic Programs/ImplementingTimers.js @@ -0,0 +1,23 @@ +// In this file we will looking at a simple program demonstrating working of timers in JavaScipt. + +// We will be creating a clock timer for 2 minutes. + +// setting time to 2 minutes +let time = 120; //Time is seconds. + +// Creating executable function +const timerFunc = function(){ + const hour = String(Math.trunc(time/3600)).padStart(2,'0'); + const min = String(Math.trunc(time/60)).padStart(2,'0'); + const sec = String(Math.trunc(time%60)).padStart(2,'0'); + // Displaying the timer + console.log(hour,":",min,":",sec); + + // Stopping the timer when it is 0 seconds. + if(time==0){ + clearInterval(timer); + } + time--; +} +const timer = setInterval(timerFunc,1000); +//Here I have set time to 1000 milliseconds as I want it to repeat it after every 1 second.