Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using an Interval to Refresh Your OData Model

DZone's Guide to

Using an Interval to Refresh Your OData Model

When using intervals, it is important to be conscience about the resources you are using on the client. Learn how to use them here.

· Big Data Zone ·
Free Resource

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

Using an interval is a way to refresh your data model without refreshing the whole DOM. The DOM is the Document Object Model, the model of your web page the browser creates.

The setInterval function allows you to define a function or expression to execute on the specified interval. The first parameter is the function; the second is the interval in milliseconds. It is important to only call the setInterval function once. Otherwise, a number of function calls will exponentially increase, which is bad.

setInterval(
    /* function or expression to execute */, 
    /* interval in ms */
);

I would recommend creating the function that updates your model or refreshes the data on your DOM outside of the setInterval function. In the function, you can call the method to update your model. Don’t forget to save the state of this before calling setInterval. The interval is set in milliseconds, so choose the amount of time that makes sense for you. For intensive data updates on the client side, you might want to have a longer interval. If you are handling all the data manipulation on the server side, you can have a smaller interval. Intervals do play into performance on the client side, so always be careful and considerate when using them.

modelServices: function() {
      var self = this;
      this.intervalHandle = setInterval(function() { 
          self.callYourFunction();
       },  30000);
}

I call my modelServices function which sets off the interval in my onInit function or after I set up the DOM. It depends on your use case. Whenever it’s appropriate to initiate your interval, call the modelServices function.

this.modelServices();

It is also important to clean up after yourself, so to speak. When your application or page the interval is on is closed, exiting, or being navigated away from, you’ll want to make sure you clear the interval so you aren’t using DOM resources when the DOM isn’t valid. You can do this on the standard onExit function.

onExit:function() {
   // You should stop the interval on exit. 
   // You should also stop the interval if you navigate out of your view and start it again when you navigate back. 
   if (this.intervalHandle) 
      clearInterval(this.intervalHandle) ;
} 

When using intervals, it is important to be conscience about the resources you are using on the client. Use intervals responsibly!

Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub.  Join the discussion.

Topics:
big data ,odata ,data modeling ,intervals

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}