Skip to content

Instantly share code, notes, and snippets.

@srujan21
Last active November 15, 2021 09:46
Show Gist options
  • Save srujan21/e2a4ec5153a3f7f8aa699557b89716be to your computer and use it in GitHub Desktop.
Save srujan21/e2a4ec5153a3f7f8aa699557b89716be to your computer and use it in GitHub Desktop.
Logic to implement timer in lightning component
startTimer : function(component, event, helper) {
console.log('Timer started >>> ')
var countDownDate = new Date(new Date().getTime() + 2 * 60000);
console.log('countDownDate **** ' + countDownDate);
// Update the count down every 1 second
var timer = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
var timeLeft = minutes + "m " + seconds + "s ";
component.set("v.timeLeft", timeLeft);
}, 1000);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment