Problem z powtarzającymi się cyframi w licznikach.

Problem z powtarzającymi się cyframi w licznikach.
K1
  • Rejestracja:około 6 lat
  • Ostatnio:ponad 4 lata
  • Postów:6
0

Czciałem zrobić 2 odliczania ,pobrałem sobie z internetu taki oto skrypt:

Kopiuj
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>

wstawiłem takie dwa liczniki przy każdym podałem inną datę do której ma odliczać.
Rezultat na stronie http://www.klasa6a148.cba.pl

edytowany 8x, ostatnio: Koder12345678910
serek
  • Rejestracja:około 11 lat
  • Ostatnio:około 4 godziny
  • Postów:1475
1

No bo nadpisujesz sobie kod, skoro nawet nazwy zmiennych nie zmieniłeś za drugim razem. Wrzuć kod do funkcji i potem z niej skorzystaj.

K1
  • Rejestracja:około 6 lat
  • Ostatnio:ponad 4 lata
  • Postów:6
0
serek napisał(a):

No bo nadpisujesz sobie kod, skoro nawet nazwy zmiennych nie zmieniłeś za drugim razem. Wrzuć kod do funkcji i potem z niej skorzystaj.

Zmieniłem sobie zmienne w kodzie i nadal nie działa,teraz się nie wyświetla 2 licznik

Kopiuj
<!-- Display the countdown timer in an element -->
<h1>Do wakacji zostało :       </h1>
<h1 id="demo"></h1>

<script>
// Set the date we're counting down to
var countDownDate = new Date("june 21, 2019 0:00:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "już są wakacje!!!";
  }
}, 1000);

</script>












<div>
<!-- Display the countdown timer in an element -->
<h1>Do zielonej szkoły zostało:  </h1>
<p id="css"></p>

<script>

// Set the date we're counting down to
var dataodliczanie = new Date("may 20, 2019 8:00:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var teraz = new Date().getTime();

  // Find the distance between now and the d
  var dystans = dataodliczanie - teraz;

  // Time calculations for days, hours, minutes and seconds
  var dni = Math.floor(distance / (1000 * 60 * 60 * 24));
  var godziny = Math.floor((dystans % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minuty = Math.floor((dystans % (1000 * 60 * 60)) / (1000 * 60));
  var sekundy = Math.floor((dystans % (1000 * 60)) / 1000);

//pokazujemy odlicznik 
  
  document.getElementById("css").innerHTML = dni + "dni " + godziny + "godziny "
  + minuty + "minuty " + sekundy + "sekundy ";
//jezeli odliczanie się skończy...
  
  if (dystans < 0) {
    clearInterval(x);
    document.getElementById("css").innerHTML = "już wyjeżdzamy na zieloną szkołę!!!";
  }
}, 1000);

</script>
</div>
edytowany 4x, ostatnio: Koder12345678910
LI
  • Rejestracja:ponad 11 lat
  • Ostatnio:ponad 3 lata
  • Postów:518
1

Użyłam tajemniczej siły i wiem że distance is not defined...


Life is Strange
K1
  • Rejestracja:około 6 lat
  • Ostatnio:ponad 4 lata
  • Postów:6
0

Thanks😀😀😀😀

serek
  • Rejestracja:około 11 lat
  • Ostatnio:około 4 godziny
  • Postów:1475
0

No ale po cholerę duplikować kod... Wrzuć to sobie do funkcji i z niej skorzystaj...

K1
  • Rejestracja:około 6 lat
  • Ostatnio:ponad 4 lata
  • Postów:6
0
serek napisał(a):

No ale po cholerę duplikować kod... Wrzuć to sobie do funkcji i z niej skorzystaj...

Żeby móc dać różne daty w licznikach

LI
  • Rejestracja:ponad 11 lat
  • Ostatnio:ponad 3 lata
  • Postów:518
1

@serek ma na myśli że jeśli napiszesz funkcję to możesz ją wywoływać z różnymi zmiennymi. Poczytaj o funkcjach są bardzo przydatne.


Life is Strange

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.