🎠setTimeout(function, milliseconds);

The setTimeout() method executes a block of code after the specified time. The method executes the code only once.

Example 1: Display a Text Once After 3 Second

// program to display a text using setTimeout method
function greet() {
    console.log('Hello world');
}

setTimeout(greet, 3000);
console.log('This message is shown first');

Example 2: Display Time Every 3 Second

// program to display time every 3 seconds
function showTime() {

    // return new date and time
    let dateTime= new Date();

    // returns the current local time
    let time = dateTime.toLocaleTimeString();

    console.log(time)

    // display the time after 3 seconds
     setTimeout(showTime, 3000);
}

// calling the function
showTime();

Last updated