trap
trap

Reputation: 2640

Angular js setInterval

How can I set and Intveral in Angularjs? I want to get after every 5 seconds the data.

My Code:

myApp.service('dataService', function ($http) {
this.getData = function () { //want to call this function after every 5 sec.
   $http({
       method: 'GET',
       url: "http://localhost:8080/api/FaceReaderData"
   }).success(function (data) {
       console.log(data)
   });
 }
});


myApp.controller('MainController', function ($scope, dataService) {
$scope.data = null;
dataService.getData(function (dataResponse) {
    $scope.data = dataResponse;
});
});

thx in advance

Upvotes: 0

Views: 734

Answers (2)

Romulo
Romulo

Reputation: 5114

I want to get after every 5 seconds the data.

You are looking for the $interval service.

Usage as follows:

var module = angular.module('dummy.module', []);

module.controller('DummyCtrl', ['$scope', '$interval', function ($scope, $interval) {

    function callback() {
        console.dir('called every 5 seconds');
    }

    $interval(callback, 5000); // callback function will be called every 5000 milliseconds

}]);

Upvotes: 4

trap
trap

Reputation: 2640

got it

myApp.controller('MainController', function ($scope,$interval, dataService) {
$scope.data = null;

$interval(function () {
    dataService.getData(function (dataResponse) {
        $scope.data = dataResponse;
    });
}, 1000); // 1 minute

});

Upvotes: 0

Related Questions