Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$interval not working for callback function, angular js

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

like image 733
Sheesh Mohsin Avatar asked Dec 06 '25 03:12

Sheesh Mohsin


1 Answers

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);
like image 131
Satpal Avatar answered Dec 07 '25 20:12

Satpal