The Wayback Machine - https://web.archive.org/web/20241121022111/https://www.geeksforgeeks.org/how-to-reload-page-after-specific-seconds-in-jquery/
Open In App

How to reload page after specific seconds in jQuery ?

Last Updated : 03 Aug, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A page can be reloaded after a specific number of seconds in jQuery using two approaches.

Using location.reload() Method: This method is used to reload the current page imitating the action of the refresh button of the browser. It has an optional Boolean parameter which can be used to denote that the page is reloaded from the server instead of being loaded from the browser cache.

The refresh code can be executed after a certain period of time using the setTimeout() function. This function has a delay parameter which denotes the time after which the function will execute. This value is given in milliseconds. The amount of time in seconds to be delayed in multiplied by 1000 to convert it to milliseconds.

The setTimeout code can be called after the document has finished loading by selecting the document object and using the ready() method on it. This will cause the page to reload after the specified amount of seconds.

  • Syntax:
    $(document).ready(function () {
          setTimeout(function () {
            alert('Reloading Page');
            location.reload(true);
          }, 5000);
        });
  • Example:




    <!DOCTYPE html>
    <html>
    <head>
      <title>
        How to reload page after
        specific seconds?
      </title>
    </head>
    <body>
      <h1 style="color: green">
        GeeksforGeeks
      </h1>
      <b>
        How to reload page after
        specific seconds?
      </b>
      <p>
        GeeksforGeeks is a computer science
        portal with a huge variety of well
        written and explained computer science
        and programming articles, quizzes and
        interview questions.
      </p>
      <p>
        The page will be reloaded in 5 seconds.
      </p>
      </script>
       
      <script type="text/javascript">
        $(document).ready(function () {
          setTimeout(function () {
            alert('Reloading Page');
            location.reload(true);
          }, 5000);
        });
      </script>
    </body>
    </html>

    
    

  • Output:
    After loading the page:
    using-reload-before

    After 5 seconds:
    using-reload-after

Using history.go(0) Method: This method loads a URL from the browser’s history depending on the parameter passed to it. If the parameter passed is ‘0’, it reloads the current page.

The refresh code can be executed after a certain period of time using the setTimeout() function. This function has a delay parameter which denotes the time after which the function will execute. This value is given in milliseconds. The amount of time in seconds to be delayed in multiplied by 1000 to convert it to milliseconds.

This code can be called after the document has finished loading by selecting the document object and using the ready() method on it. This will cause the page to reload after the specified amount of seconds.

  • Syntax:
    $(document).ready(function () {
          setTimeout(function () {
            alert('Reloading Page');
            history.go(0);
          }, 5000);
        });
  • Example:




    <!DOCTYPE html>
    <html>
    <head>
        <title>
        How to reload page after
        specific seconds?
      </title>
    </head>
    <body>
      <h1 style="color: green">
        GeeksforGeeks
      </h1>
      <b>
        How to reload page after
        specific seconds?
      </b>
      <p>
        GeeksforGeeks is a computer science
        portal with a huge variety of well
        written and explained computer science
        and programming articles, quizzes and
        interview questions.
      </p>
      <p>
        The page will be reloaded in 5 seconds.
      </p>
      </script>
      
      <script type="text/javascript">
        $(document).ready(function () {
          setTimeout(function () {
            alert('Reloading Page');
            history.go(0);
          }, 5000);
        });
      </script>
    </body>
    </html>

    
    

  • Output:

  • After loading the page:
    using-history-go-before
    After 5 seconds:
    using-history-go-after

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.



Similar Reads

How to Redirect to Another Page After 5 Seconds using jQuery ?
In this article, we will learn, how to redirect to another page or URL after a specified time in jQuery. The built-in function used for performing the action is as follows. The setTimeout(callback, delay) function takes two parameters a callback and a time in milliseconds. When this method is called, the callback function will be executed after the
1 min read
How to reload or re-render the entire page using AngularJS?
While working with AngularJS we might come across a time when we want our user to switch contexts and re-render everything again.AngularJS provides a method by which we can re-render or even reload the entire page. So in this article, we will see how we can reload the route instead of just reloading the entire page or application as refreshing the
2 min read
How to reload CSS without reloading the page using JavaScript ?
While working with CSS, you might have come across situations where you made some changes in the stylesheet and had to do a hard reload to see the changes reflected in your browser. Or maybe the style depends on some user interaction and you don't wish to hard reload the page every time. Sometimes you don't want to lose the changes made using the D
2 min read
How to reload page only once in JavaScript ?
Reloading a webpage once using JavaScript ensures that users see the most up-to-date content on their initial visit. This technique leverages session storage to track if the page has been reloaded, enhancing user experience by preventing unnecessary reloads and ensuring content freshness. Using Location.reload(true)The Location.reload(true) method
3 min read
How to send API call before page reload or close using ReactJS ?
To send an API call before the page reloads or closes using React JS we have to link or define a function when the page unloads. The page unloading happens just before the page is closed or tries to reload. PrerequisitesReact JSHTML DOM onbeforeinload eventApproachWe will use some listeners like "beforeupload" and "upload", which trigger a function
2 min read
How to hide div element after few seconds in jQuery ?
Given a div element, the task is to hide the div element after a few seconds using jQuery. Approach:Select the div element.Use delay function (setTimeOut(), delay()) to provide the delay to hide() the div element.Example 1: In this example, the setTimeOut() method is used to provide a delay to the fadeOut() method. C/C++ Code <!DOCTYPE HTML>
2 min read
Hot Reload in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime. Several powerful frameworks such as Angular, Angul
8 min read
Hot Reload in ElectronJS | Set - 2
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime. This article is a continuation of the Hot Reload i
7 min read
Automated Time-Based Reload Response in Java Web Servlet
Automated Time-Based Reload Response in Java Web Servlet comes into the picture while working with Java Servlets, we need a mechanism for page auto-reload according to our need. This automated refresh can be created in the Java servlet itself as a response. In webpages, there is a need where without clicking any button or link, the page should be a
2 min read
HTML DOM Location reload() Method
The location reload() method in HTML DOM is used to reload the current document. This method refreshes the current documents. It is similar to the refresh button in the browser.  Note: It does not return any value.  Syntax:location.reload( forceGet )Parameters: It does not take any parameters Example: This example illustrates the use of the locatio
1 min read
What’s the best way to reload / refresh an iframe?
Suppose that a user creates a webpage. Now he has a webpage, which contains an IFrame and a Button. Once the user presses the button, he/she needs the IFrame to be reloaded/refreshed. How are we going to make this possible? We are going to be finding it in the article. The following syntax is work for both cases, where the IFrame is provided &
2 min read
How to append an element in two seconds using jQuery ?
Given an HTML element and the task is append an element in the document after few seconds using fadeIn effect with the help of JQuery. Approach: Select the target element to append the element. Use one of the appendTo() or append() method to append the element. Example 1: In this example, the <div> element is appended to the <body> elem
2 min read
How to load jQuery code after loading the page?
In this article, we are going to discuss the different ways of loading the jQuery code after loading the page. The methods to accomplish this task are listed below: Table of Content Using the on() method with the load eventUsing the ready() methodUsing the load() methodMethod 1: Using the on() method with the load eventThe on() method in jQuery is
3 min read
How to fetch only sections of specific HTML areas from an external page using AJAX ?
In this article, we will see how to fetch the specific sections of an HTML file from an external page using AJAX, along with understanding their basic implementation through the illustrations. Suppose we have an HTML page and this page contains different sections. Each Section can be identified using the class name or ID. Our task is to use Ajax in
4 min read
How to get seconds since epoch in JavaScript?
Given a date, we have to find the number of seconds since the epoch (i.e. 1 January 1970, 00:00:00 UTC ). The getTime() method in the JavaScript returns the number of milliseconds since January 1, 1970, or epoch. If we divide these milliseconds by 1000 and then integer part will give us the number of seconds since epoch. Example: Input: Date = 27-0
1 min read
JavaScript Adding seconds to Date object
Given a date object and the task is to add seconds to the date using JavaScript. We will use a few methods to add seconds to the date, that are discussed below: JavaScript getSeconds() Method: This method returns the Seconds(from 0 to 59) of the provided date and time. Syntax: Date.getSeconds() Parameters: This method does not accept any parameters
3 min read
How to get the current date and time in seconds ?
In this article, we will learn how to get the current date & time in seconds using JavaScript build-in methods. We will perform it in 2 ways: Using Date.now() methodUsing new Date.getTime() methodMethod 1: Using Date.now() MethodThe Date.now() method returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. This is known a
2 min read
How to Redirects to Another WebPage if User Idle for 10 Seconds in JavaScript ?
In this article, we will create a webpage that redirects to another page if the user is idle for more than one minute. We will use JavaScript to redirect the web page to another web page if the user is ideal for 10 seconds. Approach: To make this, we will use setTimeout() and clearTimeout() methods. The setTimeout() method is used to start the coun
2 min read
Moment.js Parsing Unix Timestamp (seconds)
Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment.unix() function to parse unix timestamps (seconds). The moment.unix() function is used to create a moment using Unix Timestamp, seconds since the Unix Epoch (Jan 1 1970 12AM UTC). Syntax: moment.unix( Number ) Parameter: It takes
1 min read
Moment.js moment.duration().seconds() Method
The moment().duration().seconds() method is used to get the number of seconds of the duration. This number of seconds is calculated as a subset of a minute, therefore having a value between 0 and 59. Syntax: moment().duration().seconds(); Parameters: This method does not accept any parameters. Return Value: This method returns the seconds (0-59) of
2 min read
JavaScript Program to Convert Seconds to Milliseconds
Seconds and milliseconds are important time units for planning schedules and managing daily activities and performance optimizations. Understanding this conversion helps in better time management. // Formula to convert second into millisecond :1 second = 1 * 1000 millisecondBelow are the approaches for converting seconds into milliseconds in JavaSc
2 min read
JavaScript Program to Convert Milliseconds to Seconds
Milliseconds and Seconds are important time units for planning schedules and managing daily activities and performance optimizations. Understanding this conversion helps in better time management. // Formula to convert millisecond into second: 1 millisecond = 1 / 1000 second Below are the approaches for converting milliseconds into seconds in JavaS
2 min read
JavaScript Program to Convert Minutes into Seconds
Minutes and seconds are important for planning schedules and managing daily activities. Understanding this conversion helps in better time management. These units are essential for managing schedules, planning activities, and organizing daily routines. Example: // Formula to convert minutes into seconds:1 minute = 1 * 60 secondsBelow are the approa
2 min read
JavaScript Program to Convert Hours into Seconds
Hours and Seconds are units of time. These units are essential for managing schedules, planning activities, and organizing daily routines. Here we will convert hours into seconds using JavaScript. Example:// Formula to convert hours into seconds1 hour = 1*60 * 60 secondsBelow are the approaches for converting hours into seconds in JavaScript: Table
2 min read
How to call a function repeatedly every 5 seconds in JavaScript ?
In JavaScript, the setInterval() method allows you to repeatedly execute a function or evaluate an expression at specified intervals. This method is particularly useful for performing periodic tasks like updating a user interface or making repeated API calls. Syntax: setInterval(function, milliseconds, param1, param2, ...)Parameters: This function
2 min read
How To Remove Seconds/ Milliseconds From Date Convert To ISO String In Momemt.js?
MomentJS is useful for handling and formatting dates and times in JavaScript. To remove seconds and milliseconds from a date and convert it to an ISO string, we can use MomentJS to manipulate and format the date. This allows you to customize the date output to meet specific requirements, such as removing less relevant time components. These are the
2 min read
How to Wait n seconds in JavaScript?
In JavaScript, adding delays can be essential for managing asynchronous operations and ensuring the proper code execution sequence. Applying delays is important when waiting for a server response, creating smooth animations, or controlling timed events. These are the following ways to wait n seconds in JavaScript: Table of Content Using setTimeout(
3 min read
How to convert seconds to time string format hh:mm:ss using JavaScript ?
Converting seconds to a time string format hh:mm:ss in JavaScript involves transforming a given total number of seconds into its equivalent hours, minutes, and seconds. This helps in displaying time durations in a more readable, standard format for users. Below is the approach to convert seconds to time string format hh:mm:ss using JavaScript Table
3 min read
How to display search result of another page on same page using ajax in JSP?
In this example, we are creating a search bar to display the result on the same page in using ajax in JSP. AJAX stands for Asynchronous JavaScript and XML. Where Ajax is mainly used for Displaying another web page content on the same web page without refreshment of the page. You can read more about ajax technology here. Approach: We use the JQuery
2 min read
How can a page be forced to load another page in JavaScript ?
In JavaScript, you can force a page to load another page by using the window.location object. There are a few methods to achieve this. To force a page to load another page in JavaScript, we have multiple approaches: Below are the approaches used to force a page to load another page in JavaScript: Table of Content Using window.location.replaceUsing
2 min read
three90RightbarBannerImg