Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [HTML][JavaScript]Countdown zmiana koloru przy zakończeniu odliczania
krzesik
post
Post #1





Grupa: Zarejestrowani
Postów: 516
Pomógł: 1
Dołączył: 25.08.2012

Ostrzeżenie: (0%)
-----


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ę!
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
trueblue
post
Post #2





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Przecież od tego masz parametr onCountdownFinish.
Wywołując funkcję countdown powinieneś przekazać jako trzeci parametr funkcję (nazwę zdefiniowanej funkcji), która robi to czego oczekujesz.

Powinieneś również parametry dla new Date oraz getElementById przekazywać jako stringi, obecnie nie ma tam w ogóle znaczników ograniczających (' ").


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 23:51