ii0a_g
Last Updated: January 12, 2017
·
8.118K
· wzywno
131e3e7fc60477f8c2ca8ae8c9a60f8a

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);
    }
});
Say Thanks
Respond

2 Responses
Add your response

24259
None

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 ·
24262
131e3e7fc60477f8c2ca8ae8c9a60f8a

@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 ·