Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript]Zwiększanie liczby o losową wartość w divach
mlody69
post
Post #1





Grupa: Zarejestrowani
Postów: 183
Pomógł: 0
Dołączył: 18.05.2009

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


Witam,
powiedzmy, że mam 3 divy
  1. <div class="numbers"></div>
  2. <div class="numbers"></div>
  3. <div class="numbers"></div>

Chcę aby np po kliknięciu jakiegoś buttona w tych divach 'na zywo' odbywało się zwiększanie liczb (start np od liczbyt z przedziału 1-5) a max zdefiniowane w zmiennej.

Kod wygląda następująco:
  1. var $div = $('.numbers');
  2. var min = Math.floor(Math.random() * 5) +1;
  3. var max = Math.floor(Math.random() * 44) +1;
  4.  
  5. function getRandomInt (min, max) {
  6. return Math.floor(Math.random() * (max - min + 1)) + min;
  7. }
  8.  
  9. var interval=setInterval(run,1000);
  10.  
  11. function run(){
  12. min=getRandomInt(min+1,max);
  13. $div.html(min)
  14. if(min==max){
  15. clearInterval(interval);
  16. }
  17. }

Problem polega na tym, że w obydwu divach liczby zwiększają się tak samo a chciałbym aby wykonywało sie to niezaleznie
Go to the top of the page
+Quote Post
ber32
post
Post #2





Grupa: Zarejestrowani
Postów: 332
Pomógł: 22
Dołączył: 6.07.2010

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


Nie jest to zbyt optymalne ale może wystarczy
  1. var $div = $('#numbers'),
  2. $div1 = $('#numbers1'),
  3. $div2 = $('#numbers2');
  4. var min = Math.floor(Math.random() * 5) +1;
  5. var max = Math.floor(Math.random() * 44) +1;
  6.  
  7. function getRandomInt(min, max) {
  8. return Math.floor(Math.random() * (max - min + 1)) + min;
  9. }
  10.  
  11. var interval=setInterval(run,1000);
  12.  
  13. function run(){
  14. mini=getRandomInt(min+1,max);
  15. $div.html(mini);
  16. if(min==max){
  17. clearInterval(interval);
  18. }
  19. }
  20.  
  21. var intervalll=setInterval(run1,1000);
  22.  
  23. function run1(){
  24. miniii=getRandomInt(min+1,max);
  25. $div1.html(miniii);
  26. if(min==max){
  27. clearInterval(intervalll);
  28. }
  29. }
  30.  
  31. var intervall=setInterval(run2,1000);
  32.  
  33. function run2(){
  34. minii=getRandomInt(min+1,max);
  35. $div2.html(minii);
  36. if(min==max){
  37. clearInterval(intervall);
  38. }
  39. }

  1. <div id="numbers" class="numbers"></div>
  2. <div id="numbers1" class="numbers"></div>
  3. <div id="numbers2" class="numbers"></div>

Go to the top of the page
+Quote Post
mlody69
post
Post #3





Grupa: Zarejestrowani
Postów: 183
Pomógł: 0
Dołączył: 18.05.2009

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


Ładne to nie jest ale cóz, moze wystarczy (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
PrinceOfPersia
post
Post #4





Grupa: Zarejestrowani
Postów: 717
Pomógł: 120
Dołączył: 18.04.2009

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


"ładne nie jest" to eufemizm na to co podał ber32 (IMG:style_emoticons/default/wink.gif)

a można też tak:

EDIT:dodanie czyszczenia intervału:
Kod
var min = ~~(Math.random() * 5);
var max = 50;

$(function() {
  
   var interval = setInterval(function() {
     var counter = 0;
     var numbers = $('.numbers');
    
     numbers.each(function() {
          var number = this.number || min;
          number += ~~(Math.random() * 5) + 1;
          if (number > max) {
              number = max;
              counter++;
          }
          
          this.number = this.innerHTML = number;
      });
       if (counter >= numbers.length) clearInterval(interval);
    },1000);
});

http://jsfiddle.net/L5gbu/2/

Ten post edytował PrinceOfPersia 12.04.2014, 23:02:36
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 16.09.2025 - 00:40