0

I am trying to implement a stopwatch in a JavaScript game but the stopwatch does not start. In console, I get the error "Cannot set property 'textContent' of null" at run (monJeu.js:84)". I got the JavaScript and HTML code from the internet, and the stopwatch works perfectly by itself. It's when I try to add it to my code that it doesn't work anymore. I summon start() in btnCommencerClic () which is the start of the game and I summon pause() in gameOver(), so the end of the game. Please help ! Here is my code :

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 Answers0