Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: javascript odliczanie czasu
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
jacekpom
witajcie mam problem ze skryptem ktory odlicza czas do eventów
nie mam pojęcia jak to zrobic aby zwiększyc czas odliczania o 30 minut event bloodcastle (w kodzie nazywa sie event3) zaczyna się o 16:30 , 20:30 , 0:30, 4:30 i tak co 4 godziny

probowałem dodawać ile = (to.getTime() - now.getTime()) + 1800000; ale jak dojdzie do 0:30:00 to zegar sie resetuje a ja chciał bym aby odliczał tak do 0:00:00

oto kod:
  1. <script type="text/javascript">
  2. function liczCzas()
  3. {
  4. now = new Date();
  5. rok = now.getFullYear();
  6. miesiac = now.getMonth();
  7. dzien = now.getDate();
  8.  
  9. to = new Date(rok,miesiac,dzien,now.getHours(),now.getMinutes(),now.getSeconds());
  10. ile = to.getTime() - now.getTime();
  11. godzin = Math.floor(ile / (1000 * 60 * 60));
  12. minut = Math.floor( ile / (1000 * 60)-godzin*60);
  13. sekund = Math.floor(ile / 1000 - godzin*60*60 - minut*60);
  14.  
  15. if (godzin < 0)
  16. {
  17.  
  18. event = 0;
  19. if(now.getHours()<4 && event==0) { event = 4; }
  20. if(now.getHours()<8 && event==0) { event = 8; }
  21. if(now.getHours()<12 && event==0) { event = 12; }
  22. if(now.getHours()<16 && event==0) { event = 16; }
  23. if(now.getHours()<20 && event==0) { event = 20; }
  24. if(now.getHours()<24 && event==0) { event = 24; }
  25. to = new Date(rok,miesiac,dzien,event,0,0);
  26. ile = to.getTime() - now.getTime();
  27. godzin = Math.floor(ile / (1000 * 60 * 60));
  28. minut = Math.floor(ile / (1000 * 60)-godzin*60);
  29. sekund = Math.floor(ile / 1000 - godzin*60*60 - minut*60);
  30.  
  31. event1 = 0;
  32. if(now.getHours()<3 && event1==0) { event1 = 3; }
  33. if(now.getHours()<7 && event1==0) { event1 = 7; }
  34. if(now.getHours()<11 && event1==0) { event1 = 11; }
  35. if(now.getHours()<15 && event1==0) { event1 = 15; }
  36. if(now.getHours()<19 && event1==0) { event1 = 19; }
  37. if(now.getHours()<23 && event1==0) { event1 = 23; }
  38. if(now.getHours()<27 && event1==0) { event1 = 27; }
  39. to = new Date(rok,miesiac,dzien,event1,0,0);
  40. ile = to.getTime() - now.getTime();
  41. godzin1 = Math.floor(ile / (1000 * 60 * 60));
  42. minut1 = Math.floor(ile / (1000 * 60)-godzin1*60);
  43. sekund1 = Math.floor(ile / 1000 - godzin1*60*60 - minut1*60);
  44.  
  45. event2 = 0;
  46.  
  47. if(now.getHours()<1 && event2==0) { event2 = 1; }
  48. if(now.getHours()<5 && event2==0) { event2 = 5; }
  49. if(now.getHours()<9 && event2==0) { event2 = 9; }
  50. if(now.getHours()<13 && event2==0) { event2 = 13; }
  51. if(now.getHours()<17 && event2==0) { event2 = 17; }
  52. if(now.getHours()<21 && event2==0) { event2 = 21; }
  53. if(now.getHours()<25 && event2==0) { event2 = 25; }
  54. to = new Date(rok,miesiac,dzien,event2,0,0);
  55. ile = to.getTime() - now.getTime();
  56. godzin2 = Math.floor(ile / (1000 * 60 * 60));
  57. minut2 = Math.floor(ile / (1000 * 60)-godzin2*60);
  58. sekund2 = Math.floor(ile / 1000 - godzin2*60*60 - minut2*60);
  59.  
  60. event3 = 0;
  61.  
  62. if(now.getHours()<4 && event3==0) { event3 = 4; }
  63. if(now.getHours()<8 && event3==0) { event3 = 8; }
  64. if(now.getHours()<12 && event3==0) { event3 = 12; }
  65. if(now.getHours()<16 && event3==0) { event3 = 16; }
  66. if(now.getHours()<20 && event3==0) { event3 = 20; }
  67. if(now.getHours()<24 && event3==0) { event3 = 24; }
  68. to = new Date(rok,miesiac,dzien,event3,0,0);
  69. ile = to.getTime() - now.getTime();
  70. godzin3 = Math.floor(ile / (1000 * 60 * 60));
  71. minut3 = Math.floor(ile / (1000 * 60)-godzin3*60);
  72. sekund3 = Math.floor(ile / 1000 - godzin3*60*60 - minut3*60);
  73.  
  74.  
  75.  
  76. }
  77. document.getElementById("timef2p").innerHTML = "ChaosCastle: "+godzin+":"+minut+":"+sekund+ " <br/>IllusionTemplate: "+godzin1+":"+minut1+":"+sekund1+" <br/> DevilSquare: "+godzin2+":"+minut2+":"+sekund2+"<br/>BloodCastle: "+godzin3+":"+minut3+":"+sekund3+" ";
  78.  
  79. }
  80.  
  81. var animeInt;
  82. clearInterval(animeInt);
  83. animeInt = setInterval("liczCzas()", 1000);
  84. </script>
  85.  
  86. <b>Do rozpoczęcia na F2P:</b>
  87. <div id="timef2p"></div><br/>
PrinceOfPersia
ten kod to obraz dla czytających :/
jedna z podstawowych zasad programowania: Don't Repeat Yourself wink.gif
jeśli twój kod wygląda tak:
Cytat
if(now.getHours()<3 && event1==0) { event1 = 3; }

if(now.getHours()<7 && event1==0) { event1 = 7; }

if(now.getHours()<11 && event1==0) { event1 = 11; }

if(now.getHours()<15 && event1==0) { event1 = 15; }

if(now.getHours()<19 && event1==0) { event1 = 19; }

if(now.getHours()<23 && event1==0) { event1 = 23; }

if(now.getHours()<27 && event1==0) { event1 = 27; }

to wiedz, że coś się dzieje.
(jeśli to nie twój skrypt, to ja bym poszukał innego, bo debugowanie go zajmie chyba więcej czasu niż poszukanie lepszego skryptu, a skryptów pełno w sieci).

edit:
generalnie: jak masz kilka eventów, to powinieneś dane do eventów zapisywać w jakiejś tablicy, a nie robić zmiennych event1, event2 itp. i tysiąca warunków. Lepiej użyć pętli for.

po drugie - jaki ma być dokładnie algorytm tego skryptu?


jacekpom
na javiascript się nie znam, przerobiłem skrypt ktory znalazlem w internecie, szukałem też innych ale większość odliczała czas do jakiegos zdazenia i odliczanie sie konczyło, chciał bym poprostu aby ten ostatni event był co 4 i pół godziny czyli
16:30 , 20:30 , 0:30, 4:30 8:30, 12:30
E30Tomas
Może jak chcesz, żeby był co 4 godziny, to wykorzystaj bibliotekę Date();
czyli:

var godzinaDlaEvent1 = [16, 20, 0, 4, 8, 12]
var aktualnaData = new Date();

var aktualnaGodzina = aktualnaData.getHours();
var aktualnaMinuta = aktualnaData.getMinutes();

for(var i = 0 ; i < godzinaDlaEvent1.length ; i++){
if(aktualnaGodzina === godzinaDlaEvent1[i]){
if(aktualnaMinuta === 30){
//zdarzenie dla event'u
}
}
}
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.