在下面的代码片段中,我想定期调用函数内部的代码(以Error: Cannot pipe. Not readable.
at Busboy.Writable.pipe (_stream_writable.js:154:22)
开头,以var layout_url
结尾)。
fillLayoutData(xFactory, val.position, val.url, val.height, cType);
我怎样才能做到这一点?
答案 0 :(得分:1)
将部分代码放入您希望定期调用的函数中,并使用$ interval
调用它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)
}]);
答案 1 :(得分:0)
最简单的方法是:
$interval
服务代码示例:
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);
}
]);
附加说明:
getJSON()
函数,而不是layout_url
变量函数,因为它的值永远不会改变,因此不需要它。