The function is not running after I include the function to pause the setinterval
function using the space bar
if(event.keyCode == 32 ){
clearInterval(timerId);
The function is not running after I include the function to pause the setinterval
function using the space bar
if(event.keyCode == 32 ){
clearInterval(timerId);
All you are looking is to add an eventListner to the DOM,which can listen to key press events of keyboard
// adding eventListner to he document for keypress event
document.addEventListener("keypress", (event) => {
if (event.keyCode == 32) { // if the spacebar (keycode 32 is pressed)
clearInterval(myVar); // clear the interval
}
})
// Setting the Intevral here
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = t;
}
<div id="demo"></div>