“无法设置 null 的属性‘textContent’”错误 JavaScript 秒表

时间:2021-06-08 10:53:33

标签: javascript html stopwatch

我正在尝试在 JavaScript 游戏中实现秒表,但秒表没有启动。在控制台中,我在运行时收到错误“Cannot set property 'textContent' of null”(monJeu.js:84)”。我从互联网上获得了 JavaScript 和 HTML 代码,秒表本身完美运行。就在我尝试将它添加到我的代码中,它不再起作用。我在 btnCommencerClic() 中调用 start(),这是游戏的开始,我在 gameOver() 中调用 pause(),所以游戏结束。请帮助!这是我的代码:

window.addEventListener("load", initJeu);
let difficulte = null;
let duree = 0;
let compteur = null;
let depart;
let chrono;
let temps;
partieEnCours = false;
let ms = 0, s = 0, m = 0;
let timer;

let stopwatchEl = document.querySelector(".stopwatch");


function initJeu() {
    document.getElementById("btnCommencer").addEventListener("click", btnCommencerClic);
    repositionnerActeur();

}

function genererEntier(min, max) {
    return Math.trunc(Math.random() * (max - min + 1) + min);
}

function btnCommencerClic() {
    partieEnCours = true;
    resetObstacles();
    repositionnerActeur();
    animer();
    start();
    compteur = setInterval(incrementationTemps, 1000);
    controles(partieEnCours);
    difficulte = setInterval(niveau,1000);
    // Exemple du niveau de difficulté qui augmente avec le temps.
    majCycles(50);
    // majCycles(90);
    return true;
}
function niveau(){
    majCycles(duree);
}
function animer() {
    animerObstacles();
}
function incrementationTemps() {
    duree = duree + 1;
}
function controles(p) {
    window.addEventListener("keydown",keyboardListener);
}
function getHauteurDomObject(id) {
    let obj = document.getElementById(id);
    let objCSS = window.getComputedStyle(obj);
    return parseInt(objCSS.getPropertyValue("height"));
}

function getLargeurDomObject(id) {
    let obj = document.getElementById(id);
    let objCSS = window.getComputedStyle(obj);
    return parseInt(objCSS.getPropertyValue("width"));
}
function gameOver() {
    let domActeur = document.getElementById("acteur");
    stop();
    pause();
    domActeur.style.backgroundColor = "black";
    clearInterval(threadCreerObstacles);
    clearInterval(threadDeplacerObstacles);
    clearInterval(threadCollision);
    controles(false);
    duree = document.getElementById("stopwatch1").textContent;
    if (!estAutentifie()) {
        document.getElementById("textmodalhere").innerHTML = "Vous devez vous authentifier pour que le score soit enregistré.";
        $("#mymodal").modal("show");
    }
}
function start() {
    if(!timer) {
        timer = setInterval(run, 10);
    }
}

function run() {
    stopwatchEl.textContent = getTimer(); // this is the said line 84

    ms++;
    if(ms == 100) {
        ms = 0;
        s++;
    }
    if(s == 60) {
        s = 0;
        m++;
    }
}
function pause() {
    stopTimer();
}
function stopSlime() {
    stopTimer();
    ms = 0;
    s = 0;
    m = 0;
    stopwatchEl.textContent = getTimer();
}
function stopTimer() {
    clearInterval(timer);
    timer = false;
}
function getTimer() {
    return (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s) + ":" + (ms < 10 ? "0" + ms : ms);
}
function restart() {
    stopSlime();
    start();
}

0 个答案:

没有答案
相关问题