1
var temp = [-18 , -18.1 , -18.2, -18.3, -18.4, -18.5, -18.6,    -18.7,-18.8, -18.9,
        -19 , -19.1 , -19.2, -19.3, -19.4, -19.5, -19.6, -19.7,-19.8,  -19.9, -20];

$(".warlotemp").html(temp[0]);
$(".warlotemp").append(" C");

I want to change the selected element to be printed in the warlotemp class, i want to know if there is a way to make this

Ionut Necula
  • 11,107
  • 4
  • 45
  • 69

2 Answers2

1

You can use setInterval() and add a simple counter to it:

var temp = [-18 , -18.1 , -18.2, -18.3, -18.4, -18.5, -18.6, -18.7,-18.8, -18.9, -19 , -19.1 , -19.2, -19.3, -19.4, -19.5, -19.6, -19.7,-19.8,  -19.9, -20];
temp = temp.reverse(); //reverse the array
var counter = -1; //count start
setInterval(function() {
  if(counter < temp.length - 1){
     counter++; //increase counter
     $(".warlotemp").html(temp[counter]);
     $(".warlotemp").append(" C");
  }else{
     counter = -1; //reset counter
     $(".warlotemp").html(temp[counter]);
  }
}, 1000 * 60 * 60); //set time to one hour
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='warlotemp'>

</div>

I've also created a JSFIDDLE.

Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
  • i want to change the $(".warlotemp").html(temp[0]) to $(".warlotemp").html(temp[1]) – Diego Cervantes Dec 19 '16 at 19:55
  • i want to toggle each element – Diego Cervantes Dec 19 '16 at 19:56
  • @DiegoCervantes Please post more code in your question then. Also, how many values do you have in the array? Do you want to change one by one every one hour? – Ionut Necula Dec 19 '16 at 19:57
  • I only have 21 element in the array, i want to change the value of the or the selector of the array every hour, for changing the .warlotemp value every hour, maybe there is another way to do this thankyou – Diego Cervantes Dec 19 '16 at 19:59
  • thanks for the recomendation, but there is a way to start always with the [0] element, and then change ti every hour – Diego Cervantes Dec 19 '16 at 20:06
  • @DiegoCervantes Yes, change `var counter = 0;` to `var counter = -1;` – Ionut Necula Dec 19 '16 at 20:07
  • i changed the value to change every second, and when it reaches the end of the array it stops, is there a way to go back and forth the array without stop – Diego Cervantes Dec 19 '16 at 20:08
  • @DiegoCervantes, I've updated my answer and I've also added an jsfiddle where the timer is faster so you can see it better in action. – Ionut Necula Dec 19 '16 at 20:13
  • it works really good, but i wonder if there is a chance to go from -20, and to -19.9, -19.8 etc.. – Diego Cervantes Dec 19 '16 at 20:18
  • like going back and forth – Diego Cervantes Dec 19 '16 at 20:18
  • @DiegoCervantes, you can simply reverse the array using `temp.reverse()` for a quick solution. lAnd use the same code. – Ionut Necula Dec 19 '16 at 20:22
  • could you explain me how to do this ?? i need to run the whole code again?? – Diego Cervantes Dec 19 '16 at 20:22
  • @DiegoCervantes, no, just like `temp = temp.reverse();`. I've updated the answer again. Check it. I've also updated the fiddle. – Ionut Necula Dec 19 '16 at 20:24
  • you have been of great help, but the code only changes reverse it, but i was hoping to run it once properly and then reverse it and again like if is is going up and then comming down, im talking about temperature so it would be rare if it jumped back from -20 to -18 without a desending process – Diego Cervantes Dec 19 '16 at 20:29
  • @DiegoCervantes, you can use the same function. You just need to add it properly in the if stament. More exactly add `temp = temp.reverse();` one more time in the `else` case. I think you can handle the code from now on. – Ionut Necula Dec 19 '16 at 20:32
  • @DiegoCervantes . You're welcome. Glad I could help. Please don't forget to accept the answer. Thanks. – Ionut Necula Dec 19 '16 at 20:36
0

Let's say you have 24 elements in an array:

temp[new Date().getHours()];

Will choose the index with the current hour.

es_
  • 66
  • 7