I can not understand. how in js to do the execution interval for several objects.

For example, there is such an array

var data = { data1:{ increment:1, innerTo:"#elem" }, data2:{ increment:20, innerTo:"#elem2" }, dataN:{ increment:400, innerTo:"#elemN" } }; 

It is dynamic, that is, data can be added and deleted. How to do with a given time interval bypassing this list and for each given perform certain actions that are listed there?

for example, for data1, every second, increase the value by 1 and insert it into the element. For the second, increase every second by 2 and insert it into another element. And such a list is subject to change.

Reported as a duplicate at Grundy. javascript May 10 '18 at 11:24 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • setInterval tried? - Dmytryk
  • Already indicated in the answer :) - Zicrael
  • tried it. until it comes out - Anatoly

1 answer 1

Like this through setInterval :

 data = [ { increment:1, timer: 1000, innerTo :"elem1" }, { increment:1, timer: 2000, innerTo:"elem2" }, { increment:1, timer: 3000, innerTo:"elem3" } ] for(let el of data) { setInterval( () => { document.getElementById(el.innerTo).innerHTML = el.increment++; }, el.timer); } 
 <div id='elem1'>1</div> <div id='elem2'>1</div> <div id='elem3'>1</div> 

  • The fact is that I have come to this too. But your code is exactly like my why it inserts data into the last element. And it is necessary in each. check and make sure. that the data only go to the last. - Anatoly
  • Strange but I have everything written down, and the data goes to everything at once, as needed. - Zicrael