Forced Refresh Every Hour?

Kaede HollandKaede Holland ✭✭
edited October 2018 in Questions
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?

Comments

  • edited August 2016
    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




  • Kaede HollandKaede Holland ✭✭
    edited September 2018
    Thanks, Chris! It sounds like it would work! I just don't know how to actually do it. 
  • edited July 2017
    This seems to work. Inline Javascript, (NOT Inline snippet)
    image

    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 January 2015
    Much better than mine...  
  • edited July 2017
    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
  • Kaede HollandKaede Holland ✭✭
    edited September 2018
    Thanks, guys! A full refresh is exactly what I'm looking for. This works really nicely! 
  • edited January 2015
    My quick test had setTimeout which only runs once, BUT since I was reloading the page, it always restarted the setTimeout.

    Updating the model is the way to go...


  • edited January 2015
    Totally makes sense - forgot that the page load would (of course) restart the timer.
  • edited April 2017
    That's great stuff. Really simple and effective.
  • edited July 2017
    I am attempting to auto-refresh a page. I was able to use Seth's code above and refresh the full page, but I would really like to only refresh the model like you have suggested Chris. However, when I apply your code, everything on the page disappears.

    I am choosing 'Inline' as the Resource Location and using the following code:
    (function(skuid){
        $(document.body).one("pageload", function(){
            var myModel = skuid.model.getModel('SDSupportQueue');
            var waitTime = 3000;
            setInterval(function() {
                myModel.updateData();
            }, waitTime);
        });
    })(skuid);
    Any pointers?

    Thanks!
  • edited February 2015
    Try defining "$"
    (function(skuid){
        var $ = skuid.$;
        $(function(){
            var myModel = skuid.model.getModel('SDSupportQueue');
            var waitTime = 3000;
            setInterval(function() {
                myModel.updateData();
            }, waitTime);
        });
    })(skuid);
  • edited August 2015
    That seems to have done it. Thank you!
  • edited August 2015
    Any ideas on how to have this refresh a model that is loaded through a Page Include?
  • edited March 2015
    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.
  • edited August 2016
    Does the inline javascript code need to be saved on the parent page?

  • edited December 2016
    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?
  • Emily DavisEmily Davis ✭✭
    edited December 2016
    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
  • edited December 2016
    Thank you Emily. It worked
  • edited January 2018
    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?



  • edited July 2017
    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
  • edited July 2017
    Appreciate the tip

  • edited July 2017
    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);




  • edited October 2017
    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
  • edited October 2017
    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);
  • edited October 2017
    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
  • edited December 2017
    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.

    image

    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);
    });


  • edited October 2017
    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.
Sign In or Register to comment.