Automatically Refresh or Reload a Page using jQuery

In this section, we are going to learn reload and refresh the page automatically. We will use jQuery to do this. We can reload or refresh the embedded page or active page either manually or automatically with the help of jQuery. If we are developing a web page and we want to add auto-refresh after some time period, we can do it by using JavaScript. We can use setTimeout(), meta http-equiv tag, and setInterval() to auto-reload of a page. There are various technologies such as Codeigniter, .Net, PHP, Laravel, Java to automatically reload or refresh a web page.

Sometimes, when we work on a page, we need to reload the page after some period, and that time period can be 10 seconds, 15 seconds, 20 seconds, 30 seconds, etc. In our below example, we will describe the various processes to refresh the HTML page. This section will provide 3 examples to automatically reload a PHP page using some method and refresh the PHP page using JavaScript. If we want to refresh the page manually, we can also do it by specifying some time intervals.

Example 1:

In the first example, we are going to use the setTimeout() method to automatically reload and refresh a page. The setTimeout() method takes two parameters. In the first parameter, we will describe a function, which is used to contain code of reloading and refreshing the page. In the second argument, we will describe the time interval after which the execution of function takes place. The time interval should be in milliseconds. This method will execute the function a single time.

Example 2:

In the second example, we are going to use the setInterval() method to automatically reload and refresh a page. The setInterval() method takes two parameters. In the first parameter, we will describe a function, which is used to contain code of reloading and refreshing the page. In the second argument, we will describe the time interval, which is used to repeatedly call the function. The time interval should be in milliseconds. This method repeatedly executes the function after the given time interval until the clearInterval() is called or the window is closed. Here, we are not using any method to stop the execution of a function, which is specified in the setInterval() method. That means the method executes the function until we close the window.

Example 3:

In the third example, we are going to use Meta to automatically reload and refresh a page. Using the Meta tag, we can set the time in seconds. After that, we will target the page which we want to reload at the specified interval of time. If we want to a page into another page, we can use the URL to do this. If we don't specify the URL, we will be reloaded on the same page.

Now our above example is ready to run. When we run any of these examples, the following output will be generated:

We are from Javatpoint.com





Latest Courses