All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Wait 1 Second

Last Updated : Jul 1, 2023

JavaScript Wait 1 Second

In this tutorial we will show you the solution of JavaScript wait 1 second, here we setting some message on webpage for each 1 seconds by using setTimeout() method and it is built-in method by using this we achieve our result.

In somehow we have to make our process for particular amount of time delay we can use this concept except the delay timing because here we making delay for 1 second so our message will display after 1 second.

Step By Step Guide On JavaScript Wait 1 Second :-

Method setTimeout() is used to do some process after time limit gone or we can say after it is reach its limits.

Here we appending instruction of ‘WAIT 1 SECONDS ALERT MESSAGE WILL APPEAR!’ message on webpage after 1 second delay alert message will displayed we can also make delay with different values as per our needs it will get defined by us.

We storing our time delay on variable ‘waitt’ to value ‘1000 refers as we know 1 second’.

<!DOCTYPE html>
<html>
    <head>
        <title>wait 1 sec</title>
    </head>
    <body>
        <H3 id="demo"></H3>
        <script>
            var waitt=1000;
            document.getElementById("demo").innerHTML="WAIT 1 SECONDS ALERT MESSAGE WILL APPEAR!";
                setTimeout(() => {
                    alert("Finished 1 seconds!");
                }, waitt);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined h3 tag for with id ‘demo’ for appends instruction of ‘WAIT 1 SECONDS ALERT MESSAGE WILL APPEAR!’ message on webpage.
  7. In script file we defined variable ‘waitt’ for store value 1000. In function fun() we used setTimeout() method with waiting time as ’1 second’ so within this method what we had done will updated for each 1 seconds.
  8. After finishing 1 second we alerts user by alert message ‘Finished 1 seconds!’ and it will displayed on top center of webpage.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to make delay for one second using javascript.

When we executing this program on browser we can see message ‘WAIT 1 SECONDS ALERT MESSAGE WILL APPEAR!’ for instruct users and after 1 second alert message will appear with message ‘Finished 1 seconds!’.

We can also do some other process for each second on webpage try out something different.

I hope this tutorial on JavaScript wait 1 second helps you and the steps and method mentioned above are easy to follow and implement.