Problem z powtarzającymi się cyframi w licznikach.

0

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

<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

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.

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

<!-- 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>
1

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

0

Thanks😀😀😀😀

0

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

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

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.

1 użytkowników online, w tym zalogowanych: 0, gości: 1