Javascript set timeout function with example code

setTimeout() is a javascript function used to perform some specific task after a specific time. Like if you want to perform a task or execute a specific code after the predefined time then it will help you to do so.

Answers 3
  • SetTimeout() function in javascript

        }, 3000);

    This code snippet will display the content in the console log after the 3 seconds of page load.

  • Call setTimeout() function onclick button

    <button onclick="setTimeout(myFunction, 3000);">Try it</button>
        function myFunction() {

    It will alert the message after the 3 seconds of page-load in the browser.

  • SetTimeout() function with real example code

    <div class="alert alert-success">
        <strong>Success!</strong> Indicates a successful or positive action.
    <link rel="stylesheet" href="" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script src="" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <script src="" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script type="text/javascript">
        setTimeout(function () {
        // Closing the alert
    }, 3000);

    Just copy/paste code in the body tag of the html page. It will remove/close the alert message after 3 seconds of loading the page content.

  • Back to code snippet queries related javascript