JavaScript

How to Auto Refresh Web Page Every 5 Seconds Using JavaScript

In JavaScript, there are situations when we need to ensure that the entered content on a particular site is accurate and up to date. For instance, it is required to display the most recent content on a web page while filling out a lengthy form and observing the new changes or when you want to test a website. In such cases, auto-refreshing a web page every 5 seconds using JavaScript is very helpful in coping with these types of situations.

This article will discuss the methods to auto refresh a web page every 5 seconds using JavaScript.

How to Auto Refresh Web Page Every 5 Seconds Using JavaScript?

To auto refresh a web page every 5 seconds using JavaScript, the following approaches can be utilized:

Go through the discussed methods one by one!

Method 1: Auto Refresh Web Page Every 5 Seconds in JavaScript Using setInterval() and document.querySelector() Methods

The “setInterval()” method accesses a function at specified time interval and the “document.querySelector()” method gets the first element matching a CSS selector. These methods can be used in combination to access the specific heading tag and set the time interval to a required functionality with the help of a timer.

Syntax

setInterval(function, milliseconds, par1, par2)

In the above syntax, “function” refers to the function that needs to be accessed, “milliseconds” is the specific time interval to execute, and “par1” and “par2” are the additional parameters.

document.querySelector(CSS selectors)

Here, “CSS selectors” represent one or more than one CSS selectors.

Check out the following example.

Example

First, specify a title and a heading in the <title> and <h2> tags, respectively:

<title>Page Refresh every 5 Seconds</title>
<h2 style="text-align: left">Auto Refresh the Page</h2>

Now, set a timer value as “1”:

let timer= 1;

After that, apply the “setInterval()” method with a “1000ms” value. This will increment the timer every second. Also, access the specified heading to display it on the “Document Object Model(DOM)” upon the end of the set timer value.

Finally, iterate the value of timer with the increment of “1” using “++” post-increment operator and apply a condition in such a way that if the value exceeds 5, the “location.reload()” method will result in the reloading of the window:

setInterval(() => {
    document.querySelector('h2').innerText= timer;
    timer++;
    if(timer >5)
    location.reload();                            
}, 1000);

It can be seen that our web page gets auto refresh every five seconds:

Method 2: Auto Refresh Web Page Every 5 Seconds in JavaScript Using onload Event

The “onload” event is triggered when an object has been loaded. This technique can be implemented to refresh the page with the help of a user-defined function when the web page is loaded.

Syntax

object.onload = refreshPage(){myScript};

In the given syntax, “function” refers to the function that needs to be invoked when the object is loaded.

Look at the following example.

Example

Firstly, include a title and heading as discussed in the previous method:

<title>Page Refresh every 5 Seconds</title>
<h2>Auto Refresh the Page</h2>

Now, apply the “onload” event and invoke the function “refreshPage()” and pass “5000” as argument which indicates five seconds time interval:

<body onload= "JavaScript:refreshPage(5000);">

</body>

Lastly, define a function named “refreshpage()” with “t” as an argument which is referring to the set time for auto refreshing the web page. The “location.reload()” method will reload the page after the specified time:

function refreshPage(t){

setTimeout("location.reload(true);", t);

}

Output

Method 3: Auto Refresh Web Page Every 5 Seconds in JavaScript Using setTimeout() method

The “setTimeout()” method invokes a function after a specified set time. This method can be applied to reload a web page after a specific set timeout.

Syntax

setTimeout(function, milliseconds, par1, par2)

In the given syntax, “function” refers to the function to be accessed, “milliseconds” is the specific time interval to execute, and “par1”, “par2” are the additional parameters.

Example

In the script tag of the HTML page, apply the “setTimeout()” method in such a way when 5 seconds get passed, the location.reload() method reloads the web page:

<script>

setTimeout("location.reload(true);", 5000);

</script>

Output

We have discussed all the convenient methods to auto refresh a web page every 5 seconds using JavaScript.

Conclusion

To auto refresh a web page every 5 seconds using JavaScript, utilize the “setInterval()” and “document.querySelector()” methods for adjusting the timer value, the “refresh()” method for refreshing a web page, or the “setTimeout()” method for setting a specific timeout refresh limit of a web page. This article demonstrated the methods to auto refresh a web page every 5 seconds using JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.