- JavaScript is case sensitive. You do not agree with the case. Traditionally, JavaScript functions and variables begin with a lowercase letter and are enclosed in a camel.
- You reinitialized variables in functions that have already been initialized in the global scope.
- You did not update ui with each change in rating, only at the beginning.
Below is the updated code that should run the way you plan. It might be worth turning your point guardians into a class, as they are redundant.
Updated Html
<p id="blue"></p> <p id="red"></p> <button onclick="startA()">Start for Red</button> <button onclick="stopA()">Stop for red</button> <button onclick="startB()">Start for Blue</button> <button onclick="stopB()">Stop for Blue</button>
Updated javaScript
var startRed; var startBlue; var bluescore = 1; var redscore = 1; function startA() { stopA(); startRed = setInterval(function () { redscore++; updateUI(); }, 3000) }; function startB() { stopB(); startBlue = setInterval(function () { bluescore++; updateUI(); }, 3000) }; function stopA() { clearInterval(startRed); }; function stopB() { clearInterval(startBlue); }; function updateUI() { document.getElementById("blue").innerHTML = bluescore; document.getElementById("red").innerHTML = redscore; } updateUI();
source share