4

Does anyone know how to refresh the page only one time after 5 sec in JavaScript?

I have the following script to refresh the page after 5 sec:

setTimeout(function () {
    window.location.reload(1);
}, 5000);  // After 5 secs

But somehow this is refresh every 5 sec instead of just once time after the 1st 5 sec. Does anyone know how to fix this issue?

gcochard
  • 11,408
  • 1
  • 26
  • 41
Jin Yong
  • 42,698
  • 72
  • 141
  • 187
  • Make sure it doesn't run again each time you reload. – SLaks Jan 20 '14 at 22:53
  • You could set a cookie to see if it has already run. If the cookie is set, don't create the timeout. Better yet, use an AJAX request after 5 seconds and reload the data on the page. – Justin Wood Jan 20 '14 at 22:54
  • possible duplicate of [One time page refresh after first page load](http://stackoverflow.com/questions/6985507/one-time-page-refresh-after-first-page-load) – Felix Kling Jan 20 '14 at 22:56

4 Answers4

5

I think better solution is check document.referer and compare with document.location

if (document.referrer !== document.location.href) {
    setTimeout(function() {
        document.location.reload()
  }, 5000);
}

You can try this in jsfiddle: https://jsfiddle.net/ga0Lrurj/

PiotrT
  • 121
  • 1
  • 4
1

This is by far the simplest and lightest solution:

setTimeout(function () {
    if(window.location.hash != '#r') {
        window.location.hash = 'r';
        window.location.reload(1);
    }
}, 5000);  // After 5 secs
Ethan
  • 3,410
  • 1
  • 28
  • 49
0

You need a way to identify a reloaded page. The easiest way (imo) would be to controll this server side. Check a get-variable and include the js-code if it does not exist.

Also, I see people suggesting cookies. I don't like using cookies for minor stuff like this.

You could try something like:

// Get all get-variables
var queryDict = {}
location.search.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]})

// Check if reloaded is set
if (queryDict['reloaded'] == 'undefined') {
    // Check if url contains any get-variables
    if (Object.keys(queryDict).length == 0) {
        var redirect = document.URL + '?reloaded';
    }
    else {
        var redirect = document.URL + '&reloaded';
    }

    // The timeout
    setTimeout(function () {
        window.location = redirect;
    }, 5000);
}
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
0

You need to kept this script after doing something like insertions or updations or any other task then it will reload your page after 5 seconds for once. It works for me while uploading pictures i kept this scripts after uploading my file rather than kept it in window.load or documents.ready function its work for like a charm. here is my script. May this helps you solve your problem

<script type="text/javascript">
$(window).load(function () {
         $('#<%=FileUpload1.ClientID%>').uploadify({
        'swf': 'fy/uploadify.swf',
        'cancelImg': 'fy/uploadify-cancel.png',
        'buttonText': 'Browse Files',
        'uploader': 'Gallery.ashx',
        'folder': 'uploads',
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',

        'multi':true,
        'auto': false,

        'formData':{'paid':<%= Request.QueryString["PhotoAlbumID"]  %>},
        'onUploadStart': function (file) {
            $("#<%=FileUpload1.ClientID%>").uploadify("settings", 'Txt', $('#password').val());

            $( document ).ready(function() {


            });  setTimeout(function() { window.location=window.location;},3000);

        }  
         });
});



</script>
Kamran Khan
  • 1,042
  • 10
  • 21