简体   繁体   English

如何使setTimeout在JS中循环?

[英]how to make loop of setTimeout in JS?

I want to make an infinite loop to be able to update my chart with data posted below. 我想做一个无限循环,以便能够使用下面发布的数据更新我的图表。 I have three setTimeout() 's but how do I place them in a loop? 我有三个setTimeout() ,但是如何将它们放入循环中? Maybe some different solution is better? 也许一些不同的解决方案更好?

setTimeout(function() {
    addData(myChart, [45, 50, 30, 34, 61, 53, 42], 0, );
}, 2000);

setTimeout(function() {
    addData(myChart, [50, 40, 20, 15, 89, 63, 5], 0, );
}, 7000);

setTimeout(function() {
    addData(myChart, [45, 50, 30, 34, 61, 53, 42], 0, );
}, 10000);

Use setInterval() instead, the function passed in will get executed every n ms (here 2000 for 2 seconds). 使用setInterval()代替,传入的函数将每n ms(此处为2000,持续2秒)执行一次。 And initialize myArray with first data point. 并使用第一个数据点初始化myArray

 setInterval(function() { addData(myChart, myArray, 0, ); myArray = updateData(); // this will populate your array with new data after every 2 seconds. }, 2000); function updateData() { return updatedArray; // array with new data points } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM