Last Updated: September 09, 2019
·
12.32K
· wzywno

Page reload / refresh every 5 sec using Turbolinks - JS, Rails, JQuery

Enviroment

Rails, Turbolinks

Problem

Auto reload page without showing awful page reload to user unless something changed

Solution

$(document).on('ready page:load', function() {
    var REFRESH_INTERVAL_IN_MILLIS = 5000;
     if ($('.f-pending-message').length > 0) {
       setTimeout(function(){
        #disable page scrolling to top after loading page content
        Turbolinks.enableTransitionCache(true);

        # pass current page url to visit method
        Turbolinks.visit(location.toString());

        #enable page scroll reset in case user clicks other link
        Turbolinks.enableTransitionCache(false);
         }, REFRESH_INTERVAL_IN_MILLIS);
    }
});

2 Responses
Add your response

But where to place the code you have specified aboe ? Can you please elaborate the steps for doing that ? Thank you .

over 1 year ago ·

@huzefahuzu
In my project I had some background jobs which was triggered from website. And while the background job was running the message was presented to user (the message div has class 'f-pending-message'). In above js I check for this message and if it is present I schedule reload of page.
I put it in the the file that is included to application.js so that code is in application.js

over 1 year ago ·