Javascript set timeout function with example code
Javascript set timeout function with example code
In this session, we’ll try our hand at solving the "Javascript set timeout function with example code" puzzle by using the computer language.
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.-
SetTimeout() function in javascript
<script> setTimeout(function(){ console.log('hello'); }, 3000); </script>
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> <script> function myFunction() { alert('Hello'); } </script>
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. </div> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script type="text/javascript"> setTimeout(function () { // Closing the alert $('.alert').alert('close'); }, 3000); </script>
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.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Javascript
- Apex bar chart - fill bar color with gradient on hover
- How to use JavaScript to search for items in a list
- How to fixed bootstrap navbar at top on scroll
- Javascript to print Fibonacci series upto 15 Observations
- Check if value exists in array javascript
- Cropper js with multiple images
- How to check caps lock is on in javascript
- How to echo array in Javascript
- Uncaught TypeError: Cannot read property 'addEventListener' of undefined
- Short-circuits conditionals
- Uncaught TypeError: $(...).summernote is not a function
- How to display for slash as a string in javascript HTML
- How to set href value of anchor tag in javascript
- Animating numbers counting Javascript
- Prism js horizontally overflowing its container
- How to preview a word document in HTML using javascript
- How to remove duplicate values from array in javascript
- Uncaught TypeError: Illegal invocation
- How to get file extension using javascript/jquery
- How to display an image in Javascript onclick
- Generate 6 digit random number in javascript
- Uncaught SyntaxError: Identifier 'CODE' has already been declared
- How can I add a key/value pair to a JavaScript object
- ReferenceError: $ is not defined
- Get hostname from URL in javascript