我試圖做一個計數器,在10秒內上升到100,然後循環。我的代碼有什麼問題?爲什麼不是這個javascript計數器工作?
window.onload = function() {
var showPercent = window.setInterval(function() {
if (currentPercent < 100) {
currentPercent += 1;
} else {
currentPercent = 0;
}
document.getElementById('result').innerHTML = currentPercent;
}, 100);
};
第一個提示:看到聲明變量與JavaScript的['var'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) – Xotic750
什麼不起作用? – Steve
第二技巧:查看['console'](https://developer.mozilla.org/en/docs/Debugging_JavaScript),您將看到'未捕獲的ReferenceError:currentPercent未定義' – Xotic750