Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to refresh page automatically just once

javascript

Best Answer tomchinery, 10 April 2014 - 01:22 AM

You would use setTimeout() instead of setInterval(). More info here:

 

http://www.jquery4u....imeout-example/

 

Hope that helps, 

 

Tom :)

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 09 April 2014 - 01:49 PM

This code refreshes the page manually and automatically every 5 seconds. How can i make the page to refresh just once for 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>
 

 



#2 tomchinery

tomchinery

    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 10 April 2014 - 01:22 AM   Best Answer

You would use setTimeout() instead of setInterval(). More info here:

 

http://www.jquery4u....imeout-example/

 

Hope that helps, 

 

Tom :)



#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 10 April 2014 - 02:17 AM

YEs, that's right, you could also use a global boolean variable, set as true when the program loads. You set it to false at first refresh, then in the ready function, as it's first line, you make an if ( !booleanVar ) { return; }



#4 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 10 April 2014 - 06:02 AM

The setInterval call a location.reload...

So the script is actually called only once, but then, the page is reloaded and a new setInterval (of the new page) is called

So even changing it to setTimeout or using a global would not help, you either need to load a different page (one without the code), or saving it in a cookie


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#5 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 10 April 2014 - 01:28 PM

You are right WP, I just missed the part where you pass a parameter with the page, instead of reload you call the page with ?reload=1 in the reload, then the global checks for that parameter, if passed no reload, if not passed the reload is due.



#6 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 11 April 2014 - 02:25 PM

This solve the issue

Thanks

 

<!DOCTYPE html>
<html>
<head>
<title>Refresh Once After 5 Secs</title>
</head>
<body>

Some content

<script type="text/javascript">
//Place as last thing before the closing </body> tag
if(location.search.indexOf('reloaded=yes') < 0){
    var hash = window.location.hash;
    var loc = window.location.href.replace(hash, '');
    loc += (loc.indexOf('?') < 0? '?' : '&') + 'reloaded=yes';
    // SET THE ONE TIME AUTOMATIC PAGE RELOAD TIME TO 5000 MILISECONDS (5 SECONDS):
    setTimeout(function(){window.location.href = loc + hash;}, 5000);
}
</script>
</body>
</html>





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download