Question

I'm trying to make a timer in javascript, a very simple one. I wrote a bit of code that should have worked afaik, yet it doesn't, so I did something wrong. I cant figure out what I did wrong. after a second it changes the value of the textbox to NaN.

Here is the code:

var timeractive = false;
var tijd = 0;

function startTimer() {
    timeractive = true;
    if (timeractive == true) {
        var ticker = setInterval(function(){tijdTimer()},1000);
    }
}

function stopTimer() {
    timeractive = false;
}

function resetTimer() {
    if (timeractive == true) {
        alert("Timer is actief.");
    } else {
        alert("Timer is inactief.");
    }
}

function tijdTimer() {
    var tijd = tijd + 1;
    //var tijdstring = tijd.toString();
    document.getElementById("tijdveld").value=tijd.toString();
}

Does someone here know whats wrong?

Thanks in advance.

Was it helpful?

Solution

   var tijd = tijd + 1;
// ^^^

You are creating a new, local variable which will have the default value of undefined.

Remove the var.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top