How to add a timer on page load that stops when button is clicked?

I want to add a timer to the page where it starts on page load.

Where it goes up in milliseconds.

Then stops when the mouse is clicked on the button.

How would I create a code example of that?

That is all I am trying to do in the code.

Add a timer that starts on page load.

Goes up in milliseconds.

Then stops when the button is clicked.

https://jsfiddle.net/xvkwmndq/

I was able to do this, but how am I able to see the numbers on the screen?

// Counter
var enterDate = new Date();
function secondsSinceEnter()
{
  return (new Date() - enterDate) / 1000;
}

// Usage example
document.querySelector('button').onclick = function() {
  var sec = secondsSinceEnter();
  if (sec < 10)
      this.innerText = sec + " seconds";
  else
      this.innerText = 'You are here like for eternity';
};

Have you tried with window.onload?
window.onload

or useEffect in React…

You would want to call setInterval when page loads then in the setInterval have a function that updates the text do the time

In your button you can clearInterval to stop

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.