func" /> func" /> func"/>

Aframe changing sky every interval

252 views Asked by At

I try that my aframe-sky-component changes his image every 3sec but it´s not working. Here what i coded so far:

 <script type = "text/javascript">
   function startTimer(){
  setInterval(function(){ nextimage(); }, 3000);

   function nextimage() {
     var zahl = 0;

     if (zahl == 0) {
       $("#skyid").attr("src","#sky_2");
       zahl ++;
     }
     if (zahl == 1) {
       $("#skyid").attr("src","#sky_3");  
       zahl ++;
     }
     if (zahl == 2) {
       $("#skyid").attr("src","#sky_4");  
       zahl ++;
     }
     if (zahl == 3) {
       $("#skyid").attr("src","#sky_1"); 
       zahl ++;
     }
     if (zahl == 4) {
       zahl == 0;
     }
     }
   }
  </script>   

I guess i have some logic mistakes thx for helping :D

1

There are 1 answers

4
Piotr Adam Milewski On BEST ANSWER

Each time you call nextImage the zahl is set to 0.

You can just move it to the outer scope:

function startTimer(){
  setInterval(function(){ nextimage(); }, 3000);
  var zahl = 0;
  function nextimage() {
  ....

like I did here. Now its not zeroed by calling nextImage() so it acts like a counter.


Also i think a bit more elegant solution is having a color array:

var colors = ["blue", "red", "green", "yellow"]
function nextimage() {
  $("#skyid").attr("color", colors[zahl])
  zahl = (zahl < colors.length - 1) ? ++zahl : 0 
  //zahl is incremented until its reached the end of the array
}