JavaScript JavaScript and the DOM 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,168 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 });