Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: How to Add Main Module API to Renderer process 
Javascript :: Backbone Sync Example 
Javascript :: Include Id In Backbone 
Javascript :: allow only numbers in textbox javascript onkeypress 
Javascript :: Babel, env src decrypt, React into javascript in background 
Javascript :: ticket draw 
Javascript :: how to cut and paste an element in vanilla javascript 
Javascript :: js watchFile 
Javascript :: say something in console javascript 
Javascript :: apollo client multiple endpoints 
Javascript :: Html5 canvas resize image aspect ratio 
Javascript :: save input local storage react 
Javascript :: sort array based on subarray value 
Javascript :: concurrently package usage 
Javascript :: react js public folder image path search 
Javascript :: vite displays blank page in docker container 
Javascript :: react password check wordpress api 
Javascript :: find the minimum number in an array javascript 
Javascript :: difference between usecallback and usememo 
Javascript :: fetch devto api with api key 
Javascript :: express-roteamento 
Javascript :: with jquery Make a style menu that displays paragraphs and hides them according to the style of the slides 
Javascript :: jquery keypress div color change 
Javascript :: searchable 
Javascript :: select inputs without specific type js 
Javascript :: javascript array includes time complexity 
Javascript :: add flag persmison to write file nodejs 
Javascript :: angularjs trying to fix a rack lint error and 500 on GET /cable 
Javascript :: How can change the display of the product images on the PDP? Spartacus 
Javascript :: In React Native / Expo, is there any way to save a specific part of an image 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =