-2

所以我正在做我的一个项目,我需要一些帮助。

我希望 javascript 能够以秒为单位计数,每秒它会添加预设数量的点。

var total_points = 0
var points_per_click = 1
var points_per_second = 0

function points_per_second() {
docuement.getElementById("current_points").innerHTML("Current Points: " + total_points);
//insert here?
}

我还希望 points_per_second 能够添加到 total_points 变量中。谢谢!

4

1 回答 1

0

docuement拼写为“文档”,.innerHTML不是函数调用(将其设置为变量)& 变量和函数共享相同的命名空间, 不要设置变量points_per_second然后声明同名的函数。

对语法错误进行排序后,您可能正在寻找setInterval.

var total_points = 0;
var points_per_click = 1;
var points_per_second = 2;

function update_display(){
    var el = document.getElementById("current_points");
    el.innerHTML = "Current Points: " + total_points;
    //insert here? ... No
}

var ticker = setInterval(function(){
    total_points += points_per_second; 
    // ... or whatever your intended logic
    update_display();
}, 1000);

jsFiddle

于 2013-09-14T11:12:31.197 回答