0

i've got a Tricky problem...for now i'm working offline but soon i will get data from a web service. Now when i scroll down a javascript listener push other data on a global variable...

$(window).scroll(function() {
    if($(window).scrollTop() + $(window).height() == $(document).height()) {
           alert("CARICAAAA!");
           //window.gvariabile è la variabile globale che prenderà i risultati
           //new data li prenderò dal web service
           var newdata=["cocco","pera","melone","anguria","cocco","pera","melone","anguria","cocco","pera","melone","anguria","cocco","pera","melone","anguria","cocco","pera","melone","anguria"];
           window.gvariabile.push(newdata);
           alert(gvariabile.length);
          for(var i=0;i<newdata.length;i++){
         div.setAttribute('id2', 'd'+count); 
           div.innerHTML="<img src='img/eng_ico.jpg' class='logo'> <h3>"+newdata[i]+" "+count+"</h3><p>Dettagli</p><hr class='hor'>";
           }
          // alert("fuffa2");
          //rimetti lo scroll ad inizio pagina
           window.scrollTo(0, 1); 
       }
    });

the problem is that each time i load new data the lenght of the global variable increment of 1...not 20...(Except the first time that load correctly other 20 data)

and this is where i set for the first time the global variable:

function onDeviceReady() {

        //qua all'avvio carichiamo i primi risultati
        var risultati =["cocco","pera","melone","anguria","cocco","pera","melone","anguria","cocco","pera","melone","anguria","cocco","pera","melone","anguria","cocco","pera","melone","anguria"];
        window.gvariabile =risultati;

         for(var i=0;i< risultati.length;i++){

         var div = document.getElementById('d'+i);

         div.setAttribute('id2', 'dd'+i); 
         div.innerHTML="<img src='img/eng_ico.jpg' class='logo'> <h3>"+risultati[i]+" "+i+"</h3><p>Dettagli</p><hr class='hor'>";
         div.addEventListener("click", redirect_click(i), false);


         }

      }
2
  • Can you share your code as it is without extracting parts of it? Is the first block supposed to be a function? Or is it in the global scope? Commented Oct 6, 2015 at 14:29
  • stack prevent from insert too much code without some text....if i paste all the code it will not like it...anyway the first is a function that listen the window scroll... Commented Oct 6, 2015 at 14:32

1 Answer 1

1

Its simple, try to use concat function and set result in your variable, eg:

    var newdata = ["cocco", "pera", "melone", "anguria", "cocco", "pera", "melone", "anguria", "cocco", "pera", "melone", "anguria", "cocco", "pera", "melone", "anguria", "cocco", "pera", "melone", "anguria"];
    window.gvariable = window.gvariable.concat(newdata);
    alert(window.gvariable.length);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.