我刚刚开始学习如何从我的大学课程中编写javascript代码,我在这方面遇到了一些麻烦,看起来它应该对我有用。
var cookieAmount = 0;
var cookiePerSecond = 0;
var cookiePerClick = 1;
function firstTimeLoad(){
refreshClickStats();
}
function cookieClick(){
cookieAmount += cookiePerClick;
refreshClickStats();
}
function refreshClickStats(){
var stats = "<p>You have " + cookieAmount + " cookies.<br>You have " + cookiePerSecond + " cookies per second.</p>";
document.getElementById('cookieStats').innerHTML = stats;
}
function cookiePerSecondFunction(){
cookieAmount =+ cookiePerSecond;
refreshClickStats();
}
window.setInterval(cookiePerSecondFunction(), 1000);
问题是setInterval总是工作一次然后给我一个“无法设置属性'innerHTML'的null”。但是如果我从cookiesPerSecondFunction中评论刷新函数,它就不会给我一个错误。那么我做错了什么,1秒计时器只工作一次并在剩下的时间给我一个错误?
谢谢,
马克穆勒答案 0 :(得分:3)
这是你在找什么?
var cookieAmount = 0;
var cookiePerSecond = 0;
var cookiePerClick = 1;
function firstTimeLoad() {
refreshClickStats();
}
function cookieClick() {
cookieAmount += cookiePerClick;
refreshClickStats();
}
function refreshClickStats() {
var stats = "<p>You have " + cookieAmount + " cookies.<br>You have " + cookiePerSecond + " cookies per second.</p>";
document.getElementById('cookieStats').innerHTML = stats;
}
function cookiePerSecondFunction() {
cookieAmount = ++cookiePerSecond;
refreshClickStats();
}
window.setInterval(cookiePerSecondFunction, 1000);
<div id="cookieStats"></div>