has van
has van

Reputation: 317

angular 2 countup timer

Can some one give me count-up timer example in angular and thanks in advance

I have this TypeScript code when I add this to my project nothing happens

var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
setInterval(setTime, 1000);

function setTime() {
    ++totalSeconds;
    secondsLabel.innerHTML = pad(totalSeconds%60);
    minutesLabel.innerHTML = pad(parseInt(totalSeconds/60));
}

function pad(val) {
    var valString = val + "";
    if(valString.length < 2) {
        return "0" + valString;
    } else {
        return valString;
    }
}

Upvotes: 0

Views: 2389

Answers (2)

Nick van der Meij
Nick van der Meij

Reputation: 24

This is probably because you aren't doing it the Angular2 way. You are currently modifying the DOM directly using the document property. Yet this is a bad habit and you should do this using the build in Components and Viewbindings Angular2 supports. Instead of doing stuff like secondsLabel.innerHTML = pad(totalSeconds%60);, just use properties that you bind to your view, then it should work.

Upvotes: 0

Liyaquet Hussain
Liyaquet Hussain

Reputation: 101

You can use this directive provided by "siddii" https://siddii.github.io/angular-timer/

Upvotes: 1

Related Questions