2

This script refreshes the page manually and automaatically every 5 seconds. How can i make the page to reload the page automatically just once after 5 seconds.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Refresh or Reload a Page Using JQuery</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    </head>
    <body>
        <div><input id="btReload" type="button" value="Reload Page" /></div>
    </body>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#btReload').click(function() { location.reload(true); });    // RELOAD PAGE ON BUTTON CLICK EVENT.

            // SET AUTOMATIC PAGE RELOAD TIME TO 5000 MILISECONDS (5 SECONDS).
            setInterval('refreshPage()', 5000);
        });
        function refreshPage() { location.reload(); }
    </script>
    </html>
cwiggo
  • 2,541
  • 9
  • 44
  • 87
user3368813
  • 47
  • 2
  • 7

3 Answers3

1

use setTimeout instead of setInterval, same syntax

    <script type="text/javascript">
    $(document).ready(function() {
        $('#btReload').click(function() { location.reload(true); });   

        // SET AUTOMATIC PAGE RELOAD TIME TO 5000 MILISECONDS (5 SECONDS).
        setTimeout('refreshPage()', 5000);
    });
    function refreshPage() { location.reload(); }
Juraj Petrik
  • 895
  • 1
  • 10
  • 25
1

Perform the following modification:

var timerId = setInterval('refreshPage()', 5000);

And change the refreshPage function.

function refreshPage() { clearInterval(timerId); location.reload(); }
Alfonso Jiménez
  • 1,235
  • 1
  • 13
  • 24
0
$(document).ready(function () {
$('#btReload').click(function () { location.reload(true); });    // Manual

TimeSet(); // Auto Once});

function TimeSet() {
setTimeout(function () {
    // Do something after 5 seconds
    Refresh()
}, 5000);}

function Refresh() {
location.reload();}
Ohjay44
  • 897
  • 7
  • 20