Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
September 19, 2023
We can achieve sleep in JavaScript by using setTimeout. The setTimeout function allows you to execute a function after a specified delay in milliseconds. Here’s an example:
function myFunction() { console.log("Hello after 2 seconds!"); } setTimeout(myFunction, 2000); // This will log "Hello after 2 seconds!" after a 2-second delay.
If you need to handle more complex asynchronous operations, you can use Promises or async/await syntax.
function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function myFunction() { console.log("Start"); await sleep(2000); // This will pause execution for 2 seconds without blocking the program. console.log("End after 2 seconds!"); } myFunction();
async function myFunction() { console.log("Start"); await new Promise(resolve => setTimeout(resolve, 2000)); console.log("End after 2 seconds!"); } myFunction();