Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to Play/start or pause timer in javascript

var element = document.querySelector("targetElement");
var isPaused = false;
var time = 0;

// setInterval here = update this function every 1 second
var t = setInterval(function() {
  // if the time is not paused (isPaused is false)
  // increment current Time by 1
    if(!isPaused) {
        time++;
        element.innerText = "Seconds: " + time;
    }
}, 1000); // 1000 = 1s

// play() starts the timer
function play(){
    isPaused = false;
}
// when pause() function is called
// pause the timer and save the current time value to targeted Element or Variable
// in this case var = time
function pause(){
    isPaused = true;
}
Source by www.delftstack.com #
 
PREVIOUS NEXT
Tagged: #pause #timer #javascript
ADD COMMENT
Topic
Name
9+3 =