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);
}
});Written by wojtek
Related protips
2 Responses
But where to place the code you have specified aboe ? Can you please elaborate the steps for doing that ? Thank you .
@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
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside