Skip to content

Instantly share code, notes, and snippets.

@MartaJank
Forked from ross-u/README.md
Created November 17, 2020 15:22
Show Gist options
  • Save MartaJank/d76b21ca1d56097929b65c2117e41a2f to your computer and use it in GitHub Desktop.
Save MartaJank/d76b21ca1d56097929b65c2117e41a2f 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