comphelp
comphelp

Reputation: 525

Periodically reload a page in Rails 4 using Turbolinks

I have a page that displays some status information. This page runs on a PC without any user input and needs to refresh each few minutes.

I can get it to refresh with the meta tag but this seems crude. It forces the refresh of all the assets and causes that ugly flicker. Turbolinks does a nice job everywhere else with smarter page loading. Can I get this to work with an automatic refresh?

I'd appreciate any advice on how I can make this work better.

Upvotes: 2

Views: 1301

Answers (2)

Brock
Brock

Reputation: 41

Here is what I recently used under Turbolinks 5. Had to add some variables to stop it from reloading once the user browsed away from the page, etc.

<script>
  if(!window.reloadTimer) {
    var SECONDS = 30;
    window.reloadTimer=setTimeout(function(){
      console.log("Reload!");
      window.reloadTimer=undefined;
        Turbolinks.visit(location.toString(),{action: "replace"});
    }, SECONDS * 1000);
    document.addEventListener("turbolinks:visit",function() {clearTimeout(window.reloadTimer);window.reloadTimer=undefined;});
  }
</script>

Upvotes: 4

Arnold Roa
Arnold Roa

Reputation: 7748

Try this:

$(document).on('ready page:load', function() {
    var SECONDS = 5;
    setTimeout(function(){
        Turbolinks.enableTransitionCache(true);
        Turbolinks.visit(location.toString());
        Turbolinks.enableTransitionCache(false);
    }, SECONDS * 5);
});

Upvotes: 1

Related Questions