Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Execute Script After Specific Delay</title> </head> <body> <p>If you click the button alert will show after 2 seconds</p> <button type="button" onclick="showAlert();">Show Alert</button> <script> function showAlert(){ var delayInMilliseconds = 2000; // 2 seconds setTimeout(function() { alert("Hi there!"); }, delayInMilliseconds); } </script> </body> </html>