Created
May 3, 2018 20:25
-
-
Save tosipaulo/97b85455903c3d567f72897e7c998eaf to your computer and use it in GitHub Desktop.
CountDown.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* CountDown | |
*/ | |
const msDateCountDown = 1525303846754; //Aqui vc pega o milesegundo atual usando new Date().getTime() | |
let dateCountDown = new Date(msDateCountDown); | |
dateCountDown.setMinutes(dateCountDown.getMinutes() + 8); | |
dateCountDown = dateCountDown.getTime(); | |
let intervalDate; | |
const formatSeconds = (seconds) => { | |
if (seconds <= 9) { | |
return `0${seconds}` | |
} else { | |
return seconds | |
} | |
} | |
if (msDateCountDown) { | |
intervalDate = setInterval(() => { | |
let now = new Date().getTime(); | |
let distance = dateCountDown - now; | |
let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); | |
let seconds = Math.floor((distance % (1000 * 60)) / 1000); | |
if (distance < 0) { | |
clearInterval(intervalDate); | |
} | |
console.log(`${minutes}m${formatSeconds(seconds)}s`) // Aqui vc tem que pegar a div e dar um $(div).innerHTML = `${minutes}m${formatSeconds(seconds)}s` | |
}, 1000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment