setTimeout

The window object represents an open window in a browser.
The window object has a built-in method called setTimeout.
This method will execute a callback function (or evaluate an expression) after a specified number of milliseconds.
Here is an example using a Named Function.

function myCallbackFunction() { 
   console.log("callback");
}
window.setTimeout(myCallbackFunction, 1000);

Here is the same code using an Anonymous Function.

window.setTimeout(function() { 
   console.log("callback");
}, 1000);

Using Async

async function myAsyncFunction() { 
   const myPromise = new Promise((resolve, reject) => {
      setTimeout( () => resolve("Return This String") , 1000)
   });

   const theResult = await myPromise;

   console.log(theResult);
}
myAsyncFunction();


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext