

Using JavaScript to load a webpage after 5 seconds

The below example will illustrate how we can use the setTimeout() method to delay the web page loading time by 5 seconds − Step 3 − In this step, we will define the callback function for the setTimeout() method that contains the link to the web page that has to be loaded after the time interval. Step 2 − In the next step, we will invoke the setTimeout() method with a callback function and with a time interval to call the function. Step 1 − In the first step, we will define a function for the onclick event of the button that includes the setTimeout() method. Let us understand the practical implementation of setTimeout() method to load a web page after a delay of 5 seconds. Syntaxįollowing syntax is used to implement the setTimeout() method to delay any block of code by specific time − setTimeout(callBack_method, timeInMilliSeconds) It accepts a call back function as parameter and time limit in milliseconds, after which it calls that call back function only once and show the content of the function. The setTimeout() method is used to call a function for once after a specific time assigned to it. Let us discuss both of these methods one by one in details with code examples. We can use the following in-built methods of JavaScript to load a web page after 5 seconds − But did you know that, we can stop the web page loading for some time and delay the load time of it manually by using JavaScript and give it a desired time by which we want to delay the load. Generally, a web page loads immediately if the network connection to which user connected is very strong, and if the network connection is very poor, then it will take some time to load. We could also apply the setInterval() method to perform our task. This function waits for some seconds and then loads the web page. We use the setTimeout() function in JavaScript to load a webpage after some interval. In this tutorial, we learn to use JavaScript to load a webpage after 5 seconds.
