Forced Refresh Every Hour?

  • 7
  • Question
  • Updated 1 year ago
  • Answered
Hi there - 

Is it possible to automatically refresh a page at a specific interval? 

I am making a sweet dashboard that we would love to put up on a big screen. I'd like it to refresh itself throughout the day. 

Any ideas?
Photo of Kaede Holland

Kaede Holland

  • 2,744 Points 2k badge 2x thumb
  • happy!

Posted 4 years ago

  • 7
Photo of Chris

Chris

  • 1,632 Points 1k badge 2x thumb
Hey Kaede...

We do this by using the javascript setInterval to call a function, and then in there tell the model to updateData.  That then causes the model to refresh live (or in our case, we use skuid.model.updateData([model1, model2, ...[) since we have multiple related models.  No harsh page refresh - just a clean, client-side data update.  Does that work for you?

- Chris
Photo of Seth Vanderdrift

Seth Vanderdrift

  • 878 Points 500 badge 2x thumb
Much better than mine...  
Photo of Kaede Holland

Kaede Holland

  • 2,744 Points 2k badge 2x thumb
Thanks, Chris! It sounds like it would work! I just don't know how to actually do it. 
Photo of Seth Vanderdrift

Seth Vanderdrift

  • 878 Points 500 badge 2x thumb
This seems to work. Inline Javascript, (NOT Inline snippet)


Snippet code:
(function(skuid){
var $ = skuid.$;
$(document.body).one("pageload", function(){
// var myModel = skuid.model.getModel('MyModelId');
// var myComponent = skuid.component.getById('MyComponentUniqueId');
var waitTime = 2 * 60 * 1000; // 2 minutes time 60 seconds time 1000 milliseconds
setTimeout(function() {
window.location.reload(1);
}, waitTime);
});
})(skuid);
Update the waitTime var to amount of time you want.  NOTE: the setTimeout function uses milliseconds. 
(Edited)
Photo of Chris

Chris

  • 1,632 Points 1k badge 2x thumb
Seth's approach is very similar ... and has the pro/con of a full page reload.  A bit jarring, but also guarantees a full new page.  My approach would just update the model, and update the page inline.  I also think it might only do it once, as setTimeout I think it a single shot whereas setInterval does it over and over again?

Taking from Seth's example, it would look something like this (also updated to do it once per hour):

(function(skuid){
$(document.body).one("pageload", function(){
var myModel = skuid.model.getModel('ADD_YOUR_MODEL_NAME_HERE');
var waitTime = 60 * 60 * 1000; // 60 minutes time 60 seconds time 1000 milliseconds
setInterval(function() {
myModel.updateData();
         }, waitTime);
});
})(skuid);
I'll apologize in advance ... I didn't test this code!  Fingers crossed...  To make this easier to test, change the "waitTime" to something really short like "3000" (instead of 60*60*1000), which will be once every 3 seconds.

- Chris
(Edited)
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
The model in the page include should be the same. The model should still be accessible via the model name (skuid.model.geModel). Side note: If you're working with page includes, make sure that the model names are unique between the parent page and child page.
Photo of Griffin Brown

Griffin Brown

  • 2,766 Points 2k badge 2x thumb
Does the inline javascript code need to be saved on the parent page?
Photo of Avinash

Avinash

  • 1,726 Points 1k badge 2x thumb
How can we include more models in to this script?
We have two models on a page (where one is Basic model and aggregate model). How can i refresh both the models automatically?
(Edited)
Photo of Emily Davis

Emily Davis, Employee

  • 3,502 Points 3k badge 2x thumb
Avinash,
You should be able to do this by calling
var model1 = skuid.model.getModel('MyModel1'),
model2 = skuid.model.getModel('MyModel2'); // In the setInterval function... skuid.model.updateData([model1,model2]);
Hope that helps!
Emily
Photo of Avinash

Avinash

  • 1,726 Points 1k badge 2x thumb
Thank you Emily. It worked
Photo of Kaede Holland

Kaede Holland

  • 2,744 Points 2k badge 2x thumb
Thanks, guys! A full refresh is exactly what I'm looking for. This works really nicely! 
Photo of Glenn Elliott

Glenn Elliott, Champion

  • 7,738 Points 5k badge 2x thumb
That's great stuff. Really simple and effective.
Photo of David Forder

David Forder

  • 2,144 Points 2k badge 2x thumb
Tried this code (inline)
(function(skuid){
$(function(){
var myModel = skuid.model.getModel('ADD_YOUR_MODEL_NAME_HERE');
var waitTime = 60 * 60 * 1000; // 60 minutes time 60 seconds time 1000 milliseconds
setInterval(function() { myModel.updateData();
         }, waitTime);
});
})(skuid);



using my model name it doesnt seem to refresh at all?

Have I missed something?
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Dave,

Look for Moshe's comment in this post about defining '$' to skuid.  You need to add the 'var $ = skuid.$;' after the function declaration to skuid.

(function(skuid){
    var $ = skuid.$;

Or you can create a new inline script and Skuid will add the right boilerplate for you. Then just copy your code into the new script.

Thanks,

Bill
Photo of David Forder

David Forder

  • 2,144 Points 2k badge 2x thumb
Appreciate the tip
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
David, my guess is the $(function(){ part is not working. Try replacing this with $(document.body).one("pageload", as here:
(function(skuid){
$(document.body).one("pageload", function(){
var myModel = skuid.model.getModel('ADD_YOUR_MODEL_NAME_HERE');
var waitTime = 60 * 60 * 1000; // 60 minutes time 60 seconds time 1000 milliseconds
setInterval(function() { myModel.updateData();
         }, waitTime);
});
})(skuid);
Photo of Adam Johnson

Adam Johnson

  • 1,000 Points 1k badge 2x thumb
Hi All, we have been using this code for over a year now to refresh a case queue every second. In the last month we have been having slowness issues and have narrowed it down to this forced refresh. Removing the forced refresh from the page completely resolved the slowness issues we were having. 

The slowness issues were memory related and would build up the longer they had the page up. Which seems to point to a memory leak of some sorts. 

Does anyone have any suggestions? Is there a better way to write the snippet to account for this or is a refresh every second unrealistic? 

My gut was an every second refresh is too often, but it has worked for over a year with no issues, which makes me wonder if one of the recent updates to the platform has made this version of the snippet not function as well/cause a memory leak.

Anyway, I know this is random, but any suggestions are appreciated. 

Thank you!
Adam
Photo of Allison

Allison

  • 1,158 Points 1k badge 2x thumb
Trying to setup as inline Snippet, with model, "NewModelNotes " but not working.  Any ideas?

(function(skuid){

$(document.body).one("pageload", function(){
var myModel = skuid.model.getModel('NewModelNotes');
var waitTime = 1 * 6 * 1000; // 60 minutes time 60 seconds time 1000 milliseconds
setInterval(function() {

myModel.updateData();
         }, waitTime);
});
})(skuid);
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Allison,

This script may be working.  I would add a console.log('refresh'); under the "setInterval(function) {" line.  Then watch the console to see if the script is running.

The other thing that you may need to do is render the component where you want to see the data update.

Thanks,

Bill
Photo of John Dahlberg

John Dahlberg, Champion

  • 2,442 Points 2k badge 2x thumb
Here's the approach we're using that builds on the approaches above, but the code is parameter driven and reusable.  We use a UI only model that generates a row on load and has a field with default values for a pollModel, an updateModel and an interval (in seconds) (see Screenshot).  Then there is a Model action when a row is created it runs the snippet below.  The argument passed into the snippet is the row created when the model is loaded.



Here's the snippet:

var params = arguments[0],
$ = skuid.$;
pollmodel = params.row.PollModel;
updatemodel = params.row.UpdateModel;
pollinterval = params.row.Polling_Interval;
checkModel = skuid.$M(pollmodel);
updateModel = skuid.$M(updatemodel);
$(function(event){
    // THe names of the Models that should be checked every so often for updates
    // These should NOT be the Models associated with Charts / Tables, etc.
    var RECENT_UPDATES_MODELS = [
        pollmodel
    ];
// Each of our Models should have a Condition named "LastModifiedDate"
var COMMON_CONDITION_NAME = "LastModifiedDate";
var milliseconds = pollinterval * 1000;
var RecentUpdates = $.map(RECENT_UPDATES_MODELS,function(modelId){ return skuid.$M(modelId); });

setInterval(function(){
    var now = new Date();
    var previous = new Date(now.getTime() - milliseconds);
    $.each(RecentUpdates,function(i,model){
        var condition = model.getConditionByName(COMMON_CONDITION_NAME,true);
        var sfDateTime = skuid.time.getSFDateTime(previous);
        model.setCondition(condition,previous);
    });
    $.when(skuid.model.updateData(RecentUpdates))
        .done(function(){
           var foundModelWithUpdates = false;
           $.each(RecentUpdates,function(i,model){
               if (model.getRows().length) {
                    foundModelWithUpdates = true;
               }
           });
           if (foundModelWithUpdates) {
var modelsToUpdate = [];
if (checkModel && checkModel.getRows().length && updateModel) {
modelsToUpdate.push(updateModel); 
}
$.when(skuid.model.updateData(modelsToUpdate))
.done(function(){
    //Placeholder for future post update actions
});   
           }
        });
},milliseconds);
});
Photo of John Dahlberg

John Dahlberg, Champion

  • 2,442 Points 2k badge 2x thumb
FYI, the original source for this script was modifying this solution:  https://community.skuid.com/skuid/topics/real-time-dashboards, which has the full XML for the page.

The example opportunity dashboard uses an inline resource, and we converted it to a snippet that we can invoke more dynamically.  We use a lot of page includes in our site and have found that snippets are way more reliable than in-line code when the DOM is swapping around and loading in different sequences.  If I were to make one tweak, it would be to pull the filter field (i.e. LastModifiedDate) into the UI Model and use it as a parameter in the javascript.  That way all the business logic would be outside the code.