write a javascript function that resets a counter after an hour in javascript

index.tsx
let counter = 0;
let startTime = new Date();

function resetCounter() {
   let currentTime = new Date();
    
   if(currentTime - startTime > 3600000) { // 1 hour in milliseconds
      counter = 0;
      startTime = new Date();
   }
}

// Example usage
setInterval(() => {
  counter++;
  resetCounter();
  console.log(`Current count: ${counter}`);
}, 1000);
358 chars
19 lines

Here is an example of a JavaScript function that resets a counter after an hour. The function uses setInterval() to increment the counter at a specified interval (in this case, every second), and checks if the elapsed time since the startTime is greater than an hour. If it is, the counter is reset to 0 and the startTime is also reset. This ensures that the counter is always reset after an hour has passed.

gistlibby LogSnag