Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active November 17, 2020 15:22
Show Gist options
  • Save ross-u/a189fba109b20c04e8a6fd8d0e00b34e to your computer and use it in GitHub Desktop.
Save ross-u/a189fba109b20c04e8a6fd8d0e00b34e to your computer and use it in GitHub Desktop.
JS | Asynchronous JS and callbacks

JS | Asynchronous JS and callbacks


Console Clock - example


const second = 1000;
let counter = 0;
function concatZero(num) { // when num = 22
return ('0' + num).slice(-2); // "022" --> "22"
}
function updateClock() {
let dateObj = new Date();
const seconds = dateObj.getSeconds();
const minutes = dateObj.getMinutes();
const hours = dateObj.getHours();
// const miliseconds = dateObj.getMilliseconds();
const timeNow = concatZero(hours) + ':' + concatZero(minutes) + ':' + concatZero(seconds);
console.log(timeNow);
counter++;
if ( counter > 10 ) {
clearInterval(clockInterval)
}
}
const clockInterval = setInterval(updateClock, second);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment