Advertisement
Eonirr

Untitled

Apr 13th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. let cooldownActive = false;
  2.  
  3. function startCooldown(duration) {
  4. if (!cooldownActive) {
  5. cooldownActive = true;
  6. const endTime = Date.now() + duration;
  7. const timerElement = document.createElement('div');
  8. timerElement.style.position = 'fixed';
  9. timerElement.style.top = '10px';
  10. timerElement.style.left = '10px';
  11. timerElement.style.backgroundColor = 'black';
  12. timerElement.style.color = 'white';
  13. timerElement.style.padding = '10px';
  14. document.body.appendChild(timerElement);
  15.  
  16. const timerInterval = setInterval(() => {
  17. const timeLeft = Math.max(Math.ceil((endTime - Date.now()) / 1000), 0);
  18. timerElement.textContent = `Cooldown: ${timeLeft} second${timeLeft === 1 ? '' : 's'}`;
  19. if (timeLeft <= 0) {
  20. clearInterval(timerInterval);
  21. cooldownActive = false;
  22. timerElement.remove();
  23. }
  24. }, 1000);
  25. }
  26. }
  27.  
  28. startCooldown(5000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement