I believe the function you are looking for is setInterval(). This function will call a child function every time a certain period of time passes. In a limited use case, you can register a variable in a script and also register an interval timer to update it periodically. Depending on your use case, you may need to also write some JS to bind the new value of the number to the DOM.
Here is an example of using the intervals. In this example, clicking the button once begins the timer. After that, the interval (1000ms) will fire an event automatically to update the x variable and bind the new value to the DOM.
<!DOCTYPE html>
<html>
<body>
<button id="eventBtn" onclick="myFunction()">X = 0</button>
<script>
var x = 0;
function myFunction() {
setInterval(function(){
x++;
var btn = document.getElementById("eventBtn");
btn.innerHTML = 'X = ' + x;
}, 1000);
}
</script>
</body>
</html>
Interactive JS Fiddle for example
W3Schools - "Window setInterval() Method"