Create the original download link after checking the domain

I have put such a code in the functions.php file for the download links of my site:

// Define the function to generate the shortcode
 function generate_download_shortcode( $atts ) {

  // Get the file path from the shortcode attributes
  $file_path = $atts['file'];

  // Get the upload directory information
  $upload_dir = wp_upload_dir();

  // Get the base URL of the upload directory
  $base_url = $upload_dir['baseurl'];

  // Get the filename without extension
  $filename = basename($file_path, '.' . pathinfo($file_path)['extension']);

  // URL encode the filename
  $encoded_filename = urlencode($filename);

  // Combine base URL, index.php, and encoded filename
  $download_link = $base_url . '/index.php/' . $encoded_filename . '.' . pathinfo($file_path)['extension'] . '?token=' . md5( time() . rand() ) . '&rate=500kbps';

  return '<a href="' . $download_link . '" class="download-link">Download</a>';
}

// Add the shortcode to WordPress
add_shortcode( 'dow', 'generate_download_shortcode' );

// Add the JavaScript to handle the download link click
add_action( 'wp_footer', 'download_link_script' );

function download_link_script() {
  ?>
  <script>
  // Add event listener to the window load event
  window.addEventListener('load', function() {

  // Get all elements with the "download-link" class
  var downloadLinks = document.getElementsByClassName('download-link');

  // Add click event listener to each download link
  for (var i = 0; i < downloadLinks.length; i++) {
  downloadLinks[i].addEventListener('click', downloadLinkClickHandler);
  }

  // Function to handle download link click
  function downloadLinkClickHandler(event) {

  // Prevent default link action
  event.preventDefault();

  // Get the download link element
  var linkElement = event.target;

  // Start countdown
  var countdown = 3;
  var interval = setInterval(function() {
  linkElement.textContent = 'Download (' + countdown + ')';
  countdown--;
  if (countdown === 0) {
  clearInterval(interval);

  // Update the link element with the original download link without parameters
  linkElement.href = linkElement.href.split('?')[0];
  linkElement.textContent = 'Download';
  }
  }, 1000);

  }

  });
  </script>
  <?php
}

// Add rewrite rule to handle download requests
add_rewrite_rule('^download/([^/]+)/?', 'index.php?page_id=123&file=$matches[1]', 'top');

  
  // Function to handle download requests
  function handle_download_request() {
  
  if ( !isset($_GET['download_file']) ) {
  return; // No download request
  }
  
  $file_path = urldecode($_GET['download_file']);
  
  // Check if user has permission to access the file
  if ( !user_can( 'edit_files' ) && !file_exists($file_path) ) {
  wp_die('You do not have permission to download this file.');
  }
  
  // Generate the download link with parameters
  $download_link = generate_download_link( $file_path );
  
  // Start the download process
  startDownload($file_path);
  
  }
  
  // Add the action to handle download requests
  add_action( 'init', 'handle_download_request' );

This code will correctly apply the new link and apply the download speed to the download link I want.

Using scripting, I have created a condition that if the user clicks on the link from the main address of the site, after three seconds the token and speed limit will be removed from the link and the main link will be displayed for the user.

But my code has two big problems:
1- When the seconds count ends and a new link is created, the file must be downloaded immediately, which does not happen.

2- When the link is clicked again, the seconds counter starts counting again, which is completely wrong. If the user clicks a second time or a few more times, the file should be downloaded instead of the seconds counting down.

Please help me to solve this problem.