Limited Page View Script JavaScript

This JavaScript limited page view script restricts how long a visitor can view a page. After the time limit is reached a blocking overlay message is displayed.


Script Title: Limited Page View Script
Description: Limits how long a visitor can view a page and displays an overlay message

Live Example

After 10 seconds a blocking overlay appears on this page

Page view timer is running

Limited page view script snippet

Paste this near the end of BODY Adjust the time limit as needed

<script>
(function () {
  var maxSeconds = 10;

  function showOverlay() {
    var overlay = document.createElement("div");
    overlay.style.position = "fixed";
    overlay.style.inset = "0";
    overlay.style.background = "rgba(0,0,0,0.75)";
    overlay.style.zIndex = "999999";
    overlay.style.display = "flex";
    overlay.style.alignItems = "center";
    overlay.style.justifyContent = "center";

    overlay.innerHTML =
      '<div class="card">' +
      '<div class="card-body">' +
      '<h2>Time limit reached</h2>' +
      '<p>This page is only available for a limited time.</p>' +
      '</div></div>';

    document.body.appendChild(overlay);
  }

  setTimeout(showOverlay, maxSeconds * 1000);
})();
</script>

FAQ

No The page loads normally but becomes blocked after the time limit is reached

Yes You can replace the overlay with a redirect using window location replace