Service Portal widget $interval displays inconsistent results during refresh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
I have a widget that needs to refresh every one minute. I've got it working when executing the widget directly, however the client $interval alert does not match the server side data during it's refresh.
Client Script
api.controller=function($interval) {
/* widget controller */
var c = this;
c.getData = function()
{
c.out = "";
for(var i in c.data.metric){
c.m_name = c.data.metric[i].Name;
c.out = c.data.metric[i].Value;
alert( c.data.sample)
c.random_out = c.data.sample;
}
};
c.getData();
$interval(function() {
c.getData();
c.server.update();
}, 60000);
}
Server Script
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
var metric_result_1 = [];
try {
// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var result = sn_fd.FlowAPI.getRunner().subflow('sn_amzn_conect_spk.aws_metric_output').inForeground().run();
var outputs = result.getOutputs();
var r_num = outputs['r_num']; // String
metric_result_1 = outputs['metric_result_1']; // Array.Object
data.metric = metric_result_1;
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
data.sample = r_num;
gs.addErrorMessage((r_num))
//var out = metric_result_1.toString();
//gs.addInfoMessage(JSON.stringify(out))
})();
Server side result after refresh:
Client side result after refresh:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
Hi @heathers_ ,
The Server Script looks good to me but i feel the issue is with Client Script !!
You are calling both c.getData()
and c.server.update()
inside the $interval
but c.getData()
immediately reads from c.data
which won't be updated synchronously before you read it.
Try the below code for your cleint script:
api.controller = function($interval) {
var c = this;
c.getData = function() {
return c.server.update().then(function(response) {
c.data = response.data;
c.out = "";
for (var i in c.data.metric) {
c.m_name = c.data.metric[i].Name;
c.out = c.data.metric[i].Value;
}
c.random_out = c.data.sample;
alert(c.data.sample); // place alert here to show latest data
});
};
// Initial load
c.getData();
// Refresh every 60 seconds
var refreshInterval = $interval(function() {
c.getData(); // this will fetch latest server data and update c.data
}, 60000);
// Cancel interval on destroy
c.$onDestroy = function() {
$interval.cancel(refreshInterval);
};
};
Sandeep Dutta
Please mark the answer correct & Helpful, if i could help you.