3

This is my controller function

setInterval(function () {
var d = new Date();
var seconds = d.getMinutes() * 60 + d.getSeconds();
var fiveMin = 60 * 1;
var timeleft = fiveMin - seconds % fiveMin;
var result = parseInt(timeleft / 60) + ':' + timeleft % 60;
//console.log(result);
var timerObj= {
    timer : result
}
$scope.timerArray = timerObj;
if (timeleft === 1) { 

    $scope.statusDetails = $scope.reset;


    $scope.timeDetails();
    $scope.get_dbStatus();
    $scope.get_sosStatus();
    $scope.get_cepStatus();
    $scope.get_taskStatus();
    $scope.get_mqStatus();
    $scope.get_mrStatus();
    $scope.get_dmStatus();

}
$scope.$apply();

},500);

This is my html page

<table class="table">
                            <thead>
                                <tr>
                                    <th class="col-lg-5 col-sm-5">
                                        Name
                                    </th>
                                    <th class="col-lg-2 col-sm-2">
                                        Version
                                    </th>
                                    <th class="col-lg-1 col-sm-3">
                                        Status
                                    </th>
                                    <th class="col-lg-2 col-sm-2">
                                        Contact Info
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="status in statusDetails">
                                    <td>{{status.title}}</td>
                                    <td>{{status.apiVersion}}</td>
                                    <td><img ng-src="../public/tcup/images/{{status.status}}.png"/></td>
                                    <td>-</td>
                                </tr>
                            </tbody>
                        </table>

Timer and all the things are working fine.But each time timer gets expired and the list gets refreshed the updated list appends to the previous list.

1
  • 2
    Please change setInterval to $Intervel Commented Apr 18, 2015 at 6:48

2 Answers 2

2

You need to kick off new digest cycle because Angular doesn't know you changed something:

setInterval(function () {
    var d = new Date();
    var seconds = d.getMinutes() * 60 + d.getSeconds();
    var fiveMin = 60 * 2;
    var timeleft = fiveMin - seconds % fiveMin;
    var result = parseInt(timeleft / 60) + ':' + timeleft % 60;
    //console.log(result);
    var timerObj = {
        timer: result
    }
    $scope.timerArray = timerObj;
    console.log("timer...........");
    console.log($scope.timerArray.timer);

    $scope.$apply(); // <--- apply changes to models
}, 500);
Sign up to request clarification or add additional context in comments.

4 Comments

Glad to heat it. Feel free to accept one of the answers, the one with $interval is also correct.
one question-"I want to call a another functon after the time expires how can i do that"
using clearTimeout() or clearInterval() it stops the timer rather than resetting it.
0

Instead of setInterval, use $interval to run digest cycle for you, after function execution completed.

Code

$interval(function () {
    var d = new Date();
    var seconds = d.getMinutes() * 60 + d.getSeconds();
    var fiveMin = 60 * 2;
    var timeleft = fiveMin - seconds % fiveMin;
    var result = parseInt(timeleft / 60) + ':' + timeleft % 60;
    //console.log(result);
    var timerObj = {
        timer: result
    }
    $scope.timerArray = timerObj;
    console.log("timer...........");
    console.log($scope.timerArray.timer);
}, 500)

Edit 1

To call method after interval gets completed you need to call that inside its promise chain, you could restrict you $interval to call it specified amount of time by adding 3rd parameter which will run $interval function that amount of time.

Code

var interval = $interval(function () {
    var d = new Date();
    var seconds = d.getMinutes() * 60 + d.getSeconds();
    var fiveMin = 60 * 2;
    var timeleft = fiveMin - seconds % fiveMin;
    var result = parseInt(timeleft / 60) + ':' + timeleft % 60;
    //console.log(result);
    var timerObj = {
        timer: result
    }
    $scope.timerArray = timerObj;
    console.log("timer...........");
    console.log($scope.timerArray.timer);
}, 500, 10);

interval.then(function(){
    //this code will execute after interval completion
    //you could call any scope method from here
});

Edit 2

If you call your callback function only once then you should not rely on $interval , better you could use $timeout instead $interval, It will run only once.

Code

var timeout = $timeout(function() {
    $scope.statusDetails = [];
    $scope.timeDetails();
    $scope.get_dbStatus();
    $scope.get_sosStatus();
    $scope.get_cepStatus();
    $scope.get_taskStatus();
    $scope.get_mqStatus();
    $scope.get_mrStatus();
    $scope.get_dmStatus();
}, 120000);
timeout.then(function() {
    $scope.repeat()
});

Edit 3

Basically you will need to add one method which will call on the button click using callInterval which will start the $interval & inside that we will maintain intervalPromise variable which will check if interval already exists then it will cancel that it interval & then it will create a new interval

Code

var intervalPromise;

$scope.callInterval = function() {
    if (intervalPromise) //remove interval if already exists
        $interval.cancel(intervalPromise);
    intervalPromise = $interval(function() {
        //..your existing code here
    }, 500);
};

13 Comments

one question-"I want to call a another functon after the time expires how can i do that"
@sanmoypaul $interval will run after each 500 milliseconds, for running for number of times you could pass 3rd option as count like $interval(function () {//code here }, 500, 10) as second parameter is 10 It will run interval 10 times, to run it only once use $timeout..see my update in answer
$scope.repeat = function(){ setInterval(function(){ $scope.statusDetails = []; $scope.timeDetails(); $scope.get_dbStatus(); $scope.get_sosStatus(); $scope.get_cepStatus(); $scope.get_taskStatus(); $scope.get_mqStatus(); $scope.get_mrStatus(); $scope.get_dmStatus(); }, 120000) } I want to call this function after the timelimit of 2mins gets expired.
After the timelimit in the count down timer expires $scope.repeat will be called.
Please see this jsfiddle.net/arunpjohny/9v45oa3g/1
|

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.