Witam,

mam funkcję, która pokazuje ilość pozostałego czasu odliczając go wstecz.
W jaki sposób zrobić, aby przy zakończonym odliczaniu zmienił się kolor czcionki?
  1. <span style="font-family: Digital-7 Mono; font-size: large; color: green;"><strong id="<? echo $counter1; ?>"></strong></span>
  2.  
  3. function countdown(targetDate, displayElement, onCountdownFinish /* = null*/) {
  4. if (!(targetDate && displayElement)) {
  5. return;
  6. }
  7. var formatTimeInterval = function(seconds) {
  8. var hrs = Math.floor(seconds / 3600)
  9. var min = Math.floor(seconds / 60) % 60;
  10. var sec = seconds % 60;
  11. return (hrs + ':' + min + ':' + sec).replace(/(^|:)(\d)(?=:|$)/g, '$10$2');
  12. };
  13. var refreshTimer = function() {
  14. var now = new Date();
  15. var diffMilliseconds = targetDate.getTime() - now.getTime();
  16. var diffSeconds = Math.round(diffMilliseconds / 1000);
  17. if (diffSeconds < 0) {
  18. diffSeconds = 0;
  19. }
  20. var countdownHTML = formatTimeInterval(diffSeconds)
  21. if (countdownHTML != displayElement.innerHTML) {
  22. displayElement.innerHTML = countdownHTML;
  23. }
  24. if (diffSeconds === 0) {
  25. clearInterval(intervalId);
  26. if (typeof onCountdownFinish === 'function') {
  27. onCountdownFinish(targetDate);
  28. }
  29. }
  30. };
  31. var intervalId = setInterval(refreshTimer, 250);
  32. refreshTimer();
  33. }
  34.  
  35. (function() {
  36. var now = new Date();
  37. countdown(new Date(<? echo $Aktual; ?> + <? echo $czas; ?> * 1000), document.getElementById(<? echo $counter1; ?>));
  38. })();
  39.  
  40.  
  41.  


po dodaniu

  1.  
  2. <span [b]id="<? echo $counter2; ?>"[/b] style="font-family: Digital-7 Mono; font-size: x-large; color: green;"><strong id="<? echo $counter1; ?>"></strong></span>
  3. .....
  4.  
  5. var refreshTimer = function() {
  6. var now = new Date();
  7. var diffMilliseconds = targetDate.getTime() - now.getTime();
  8. var diffSeconds = Math.round(diffMilliseconds / 1000);
  9. if (diffSeconds < 0) {
  10. diffSeconds = 0;
  11. [b]const divElement = document.getElementById("<? echo $counter2; ?>");
  12. divElement.style.color = "red";[/b]
  13.  
  14. }
  15. ......


gdzie $counter2 jest identyfikatorem rekordu, owszem kolor zmienia się na czerwony, ale dopiero po odświeżeniu strony, idealnie by było jak zegar dochodzi do 00:00:00 i automatycznie zmienia się na czerwony.
Poproszę o podpowiedzi, dziękuję!