setTimeout() setInterval()
by trentHarlem
HTML
<p id='demo'>
</p>
<p>
setTimeout() <br>
runs a function ONCE after a specified time has elapsed
</p>
<p>
setInterval() <br>
runs a function REPEATEDLY with a fixed time delay between each call
</p>
<p>
requestAnimationFrame()<br>
The modern version of setInterval(). Executes a specified block of code before the browser next repaints the display, allowing an animation to be run at a suitable framerate regardless of the environment it is being run in.
</p>
JavaScript
let myGreeting = setTimeout(() => {
console.log('Sup, Brah!');
}, 3000);
clearTimeout(myGreeting); // pass the name of the timeout to cancel
//let name = 'Gern'
/* let myGreeting2 = setTimeout((name) => {
name ? console.log(`Hello, ${name}!`) :
console.log('Hello!');
}, 3000); */
/* let myGreeting3 = setTimeout((name = 'Gern Blanstein') => {
name ? console.log(`Hello, ${name}!`) :
console.log('Hello!');
}, 3000); */
function displayTime() {
let date = new Date();
let time = date.toLocaleTimeString(); // international api
document.getElementById('demo').textContent = time;
//console.log(time);
}
const createClock = setInterval(displayTime, 1000);