0

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

2 Answers 2

4

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

}]);
Sign up to request clarification or add additional context in comments.

Comments

0

got it

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

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

});

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.