3

I am a beginner of angularjs, sorry if i asked silly question.

function getCams(callback){
  var media_list = [];
  MediaStreamTrack.getSources(function(sourceInfos){
    var i=0;
    while(i!=sourceInfos.length){
      if (sourceInfos[i].kind == 'video'){
        var temp = [];
        temp.push(sourceInfos[i].id);
        temp.push(sourceInfos[i].label);
        media_list.push(temp);
      }
      i++;
    }
    callback(media_list);
  });
}
var app = angular.module('myApp', []).config(function($interpolateProvider){
    $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
});
app.controller('myCtrl', function($scope, $interval) {
  $scope.cams = [];
  var checkcams = getCams(function(media_list){
    $scope.cams=media_list;
    $scope.$apply();
    console.log("test");
  });
  $interval(checkcams, 10000);
});

Above is the code from where i am trying to get the number of cams connected to a system, and trying to update the same in angular js using callback function, In this line

$interval(checkcams, 10000);

I am trying to call that function after every 10 secs but this function run only once after the page load, and not running after every 10 secs.

I already have a look at this question, it wont help me out. $interval not running, angularjs

2
  • getCams is not returning anything, the $interval is not working. This is expected behavior. Commented Sep 2, 2015 at 9:50
  • So, how to rewrite this code in proper way to update the angular js scope perfectly? Commented Sep 2, 2015 at 9:55

2 Answers 2

1

getCams is returning nothing hence $interval is not working. This is expected behavior.

You can rewrite your code as

//Wrap getCams call in a function
var checkcams = function(){
    getCams(function(media_list){
        $scope.cams=media_list;
        $scope.$apply();
        console.log("test");
    });
}

//Call it when view is launched
checkcams();

//set Interval
$interval(checkcams, 10000);
Sign up to request clarification or add additional context in comments.

3 Comments

So, only one line is different in this code, what exactly this line is doing here? checkcams(); I want to know the concept behind.
@SheeshMohsin, Only one line is not different, I have wrapped getCams function call in a function and then passed the refrence of that function to interval. read my inline comments
Yeah, Got it, let me try
0

By doing this

var checkcams = getCams(function(media_list){
$scope.cams=media_list;
$scope.$apply();
console.log("test");

});

You have set checkcams to be a variable of whatever getCams returns, not a function.

Try this instead

function checkcams () {
    getCams(function(media_list){
        $scope.cams=media_list;
        $scope.$apply();
        console.log("test");
    });
}
$interval(checkcams, 10000);

Comments

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.