Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript and the DOM (Retiring) Getting a Handle on the DOM Using CSS Queries to Select Page Elements

duke Beardslee
duke Beardslee
3,361 Points

how do get a sleep function in javascript

im trying to make my program wait for 15 seconds and then alert a message.

2 Answers

Ben Reynolds
Ben Reynolds
35,170 Points

Try the setTimeout method:

setTimeout(function(){
     // Code here will run after the timeout 
}, 15000);  // Time in milliseconds
duke Beardslee
duke Beardslee
3,361 Points

It didn't work, here is my code below.

const myButton = document.getElementById('start');

start.addEventListener('click', () => { setTimeout(function(){ alert("hello"); }, 15000); // Time in milliseconds });