增加值/脚本

时间:2013-12-18 09:40:47

标签: javascript timer increment

我需要一些帮助,每秒增加值/ 1。

我有

<script>
function incrementValue()
{
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('number').value = value;
return false;
}
</script>

现在我需要一个能增加'number'值的函数,并开始每秒加+1,直到我点击另一个图像。

我唯一能做的就是:

function incrementvale()
var incrementTime = 1;
var incrementBy = 1;
private var counter = 0;

function Start () {
InvokeRepeating("Increment", incrementTime, incrementTime);
}

function Increment () {
counter += incrementBy;
}

哪个是可怕的错! 问题!

当我点击图片时。

功能开始。

该函数每秒将'number'的值增加1。

由于

1 个答案:

答案 0 :(得分:0)

如果你真的想在1秒后准确地增加一个值,我建议你保存开始时间(Unix时间戳,例如自1970年1月1日以来的秒数)。如果你想知道增量变量的值只是从起始值中减去。

因为您提到了PHP并向我们展示了JS Code,所以我将它保存在Pseudo-Code

function getUnixTimeStampNow() {
    return Math.round((new Date()).getTime() / 1000);
}

var start = getUnixTimeStampNow();

// at any time do

var current_value = getUnixTimeStampNow() - start;