简体   繁体   English

Javascript Timer返回NaN

[英]Javascript Timer returns NaN

I'm trying to make a timer in javascript, a very simple one. 我正在尝试使用javascript做一个计时器,这是一个非常简单的计时器。 I wrote a bit of code that should have worked afaik, yet it doesn't, so I did something wrong. 我写了一些应该可以使用afaik的代码,但是没有,所以我做错了。 I cant figure out what I did wrong. 我不知道我做错了什么。 after a second it changes the value of the textbox to NaN. 一秒钟后,它将文本框的值更改为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. 提前致谢。

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

You are creating a new, local variable which will have the default value of undefined . 您正在创建一个新的局部变量,其默认值为undefined

Remove the var . 删除var

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM