I need a button that will show and update the time each second since clicked. If it's clicked again it's supposed to show nothing. I tried so much (switch etc.) but i can't solve it. If i click on the button it shows the time and updates it but if i click it again it emptys the innerHTML of the paragraph but a second later the time pops up again and updates itself. Can someone tell me please why does my clearInterval does not work and how i can fix this?
var abc = false;
function switched() {
let x = document.getElementById("switch");
if (!abc) {
var myFun = setInterval(Time, 1000);
abc = true;
return;
}
if (abc) {
clearInterval(myFun);
abc = false;
x.innerHTML = "";
return;
}
function Time() {
var date1 = new Date();
var t = date1.toLocaleTimeString();
x.innerHTML = t;
}
}
<p>On switch which shows you're current time:</p>
<button onclick="switched()">On</button>
<p id="switch"></p>