Wednesday, September 26, 2012

Auto refresh page with Javascript & Meta tag

1. Using meta tag, for example to auto refresh to your url every 1 second.
<meta http-equiv="refresh" content="1;url='http://your_url' " />

2. Using Javascript, for example to auto refresh to your url every 1 second:
<script>
  function auto_refresh() {
    window.location = 'http://your_url';
  }
  setTimeout(auto_refresh, 1000);
</script>


3. Using Javascript + jQuery to auto refresh when having no activity in some minutes (e.g. 1 minute)
<script>
     var time = new Date().getTime();
     $(document.body).bind("mousemove keypress", function(e) {
         time = new Date().getTime();
     });

     function auto_refresh() {
         if(new Date().getTime() - time >= 60000)
             window.location.reload(true);
         else
             setTimeout(auto_refresh, 10000);
     }

     setTimeout(auto_refresh, 10000);
</script>


No comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!