Reputation: 227
In the following code snippet, I would like to call the code inside the function at intervals (starting with var layout_url
and ending with fillLayoutData(xFactory, val.position, val.url, val.height, cType);
).
rp3.controller('c1Ctrl', [
'$scope',
'xFactory',
function($scope, xFactory) {
var layout_url = "json/dashboard/layout/mpu/layout.json";
xFactory.getJSON(layout_url, function(layout) {// read layout's web
// service
$.each(layout, function(i, val) {
chart.push({
"v" : val,
"x" : xFactory
});
var cType = getChartType(val.chartType);
// alert(cType);
drawLayout(parentDIV.name, val.position, val.width,
val.height, val.title, val.color, val.bgcolor,
buttomCtrl.withCtrl, cType);
fillLayoutData(xFactory, val.position, val.url, val.height,
cType);
});
}, function() {
console.log("Connection! ");
});
} ]);
How can I achieve this?
Upvotes: 0
Views: 143
Reputation: 1570
The most simple way to do that is:
$interval
service in the controller Code example:
rp3.controller('c1Ctrl', ['$scope','xFactory','$interval'
function($scope, xFactory, $interval) {
var layout_url = "json/dashboard/layout/mpu/layout.json";
//1. Wrap all the necessary code inside a method
$scope.foo = function(){
xFactory.getJSON(layout_url, function(layout) { // read layout's web
// service
$.each(layout, function(i, val) {
chart.push({
"v": val,
"x": xFactory
});
var cType = getChartType(val.chartType);
// alert(cType);
drawLayout(parentDIV.name, val.position, val.width,
val.height, val.title, val.color, val.bgcolor,
buttomCtrl.withCtrl, cType);
fillLayoutData(xFactory, val.position, val.url, val.height,
cType);
});
}, function() {
console.log("Connection! ");
});
};
//2. use $interval service. I use 2s delay (2000ms)
$interval($scope.foo, 2000);
}
]);
Additional note:
getJSON()
function, not the layout_url
variabile, as it's not needed since its value never change.Upvotes: 0
Reputation: 6393
put part of your code in function which you want call at intervals and call it with $intervals
rp3.controller('c1Ctrl', [
'$scope',
'xFactory',
'$interval',
function ($scope, xFactory, $interval) {
var layout_url = "json/dashboard/layout/mpu/layout.json";
$scope.myIntervalFunction = function () {
xFactory.getJSON(layout_url, function (layout) {// read layout's web
// service
$.each(layout, function (i, val) {
chart.push({
"v": val,
"x": xFactory
});
var cType = getChartType(val.chartType);
// alert(cType);
drawLayout(parentDIV.name, val.position, val.width,
val.height, val.title, val.color, val.bgcolor,
buttomCtrl.withCtrl, cType);
fillLayoutData(xFactory, val.position, val.url, val.height,
cType);
});
}, function () {
console.log("Connection! ");
});
}
var interval = $interval(function(){
return $scope.myIntervalFunction()
},100)
}]);
Upvotes: 1