Your approach effectively solves the problem by:

  1. Storing a precise timestamp (either in the database or in a state)
  2. Comparing it with the current time
  3. Implementing conditional logic based on this difference

To implement this solution:

// Option 1: Using a custom state
// At timer start
let startTime = Date.now();
// Store in state
$w.state.startTime = startTime;

// To check (on page load or user action)
let currentTime = Date.now();
let elapsedSeconds = Math.floor((currentTime - $w.state.startTime) / 1000);

if (elapsedSeconds >= 60) {
  // Time's up - lock functionality 
} else {
  // Display remaining time: 60 - elapsedSeconds
}

This method works perfectly regardless of whether the user leaves the page or stays on it, as time continues to advance independently of user activity.

No need for complex workflows, plugins, or interval timers that are susceptible to browser throttling. This approach is both simpler and more reliable.

1 Like