Learning JavaScript — Part 18

Jeff P
Oct 13, 2022

--

setTimeout

This method can be used when you want something to happen after a certain time. The argument of the amount of time is expressed in milliseconds (1s = 1000ms)

For example:

setTimeout(function() {
console.log(`It's been five seconds!!!`)
}, 5000);

setInterval

This method will run forever….

setInterval(function() {
let i = new Date();
console.log(`The Date and time is now ${i}`)

}, 1000);

which results in

The Date and time is now Thu Oct 13 2022 16:09:39 GMT+0100 (British Summer Time)
The Date and time is now Thu Oct 13 2022 16:09:40 GMT+0100 (British Summer Time)
The Date and time is now Thu Oct 13 2022 16:09:41 GMT+0100 (British Summer Time)
The Date and time is now Thu Oct 13 2022 16:09:42 GMT+0100 (British Summer Time)
The Date and time is now Thu Oct 13 2022 16:09:43 GMT+0100 (British Summer Time

forever…

--

--

Jeff P
Jeff P

Written by Jeff P

I tend to write about anything I find interesting. There’s not much more to it than that really :-)

No responses yet