0

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?

1
  • Expose that portion of the code as a controller method, and you can do anything you want with it. Commented Apr 24, 2016 at 21:51

2 Answers 2

1

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) 
}]);
Sign up to request clarification or add additional context in comments.

Comments

0

The most simple way to do that is:

  • injecting the $interval service in the controller
  • wrap all theecessary code inside a method.
  • use the $interval service to call the method and set a delay

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:

  • You need to just wrap the getJSON() function, not the layout_url variabile, as it's not needed since its value never change.

3 Comments

Make sure you cancel the $interval on $scope.$destroy.
What if I want to update only fillLayoutData(xFactory, val.position, val.url, val.height, cType);? I tried to move the function to inside the loop but this causes the code to update only the last chart.
@BlairOsbay is I understood correctly what your problem is, it's probably because you have the each loop which iterates from the first to the last item, before the interval actually start. So because of that you see it applied only on the last item. Can you edit your original post including this new problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.