[英]How do I refresh the contents of an array?
I have an array which is taking values over the space of a second and calculating the average. 我有一个数组,它在一秒钟的时间内获取值并计算平均值。 The array is then permanently storing the values. 然后,该数组将永久存储值。 I need them to remove the values after the average has been calculated. 计算平均值后,我需要它们删除这些值。 I have tried clearing the array at the end of the calculateAverage
function using levelMeasurements = [];
我尝试使用levelMeasurements = [];
在calculateAverage
函数的末尾清除数组levelMeasurements = [];
and levelMeasurements = 0;
和levelMeasurements = 0;
but that does not work. 但这不起作用。
Any help on this would be greatly appreciated, thanks! 任何帮助,将不胜感激,谢谢!
My code: 我的代码:
var levelMeasurements = [];
function collectLevelMeasurements() {
levelMeasurements.push(averagingLevel);
}
var collectInterval = window.setInterval(collectLevelMeasurements, 0);
function calculateAverage() {
window.clearInterval(collectInterval);
var avg = 0;
for (counter = 0; counter < levelMeasurements.length; counter++) {
avg += levelMeasurements[counter] / levelMeasurements.length;
}
averageAbsoluteLevel = Math.abs(avg);
averageDbLevel = Tone.gainToDb(averageAbsoluteLevel) * scale + offset;
console.log("Measure for 5 minutes: average level is:" + averageDbLevel);
}
window.setInterval(calculateAverage, 1000);
You can try following: 您可以尝试以下操作:
levelMeasurements.length = 0.
Or refer more solution at here: How do I empty an array in JavaScript? 或在此处参考更多解决方案: 如何在JavaScript中清空数组?
Your collectInterval
timeout is never reinstated after the first time calculateAverage
is called, unless the issue is something else not visible from the code. 第一次调用calculateAverage
之后,永远不会恢复您的collectInterval
超时,除非问题在代码中不可见。
In your code calculateAverage
is called every 1000 milliseconds, but the values stored in levelMeasurements are not updated after the first window.clearInterval
. 在您的代码中,每1000毫秒调用一次calculateAverage
,但是存储在levelMeasurements中的值在第一个window.clearInterval
之后不会更新。
I made a simplified snippet without Tone lib: 我做了一个没有Tone lib的简化代码段:
var levelMeasurements = []; //added this as a easy source of values let count = 0; function collectLevelMeasurements() { // levelMeasurements.push(averagingLevel); levelMeasurements.push(count++); } var collectInterval = window.setInterval(collectLevelMeasurements, 0); function calculateAverage() { window.clearInterval(collectInterval); var avg = 0; for (counter = 0; counter < levelMeasurements.length; counter++) { avg += levelMeasurements[counter] / levelMeasurements.length; } averageAbsoluteLevel = Math.abs(avg); averageDbLevel = averageAbsoluteLevel; //prolly dont need tone lib for this example // averageDbLevel = Tone.gainToDb(averageAbsoluteLevel) * scale + offset; console.log("Measure for 5 minutes: average level is:" + averageDbLevel); //reset these variables levelMeasurements = []; count = 0; console.log(levelMeasurements)//empty array at this point //reset the collectInterval! collectInterval = window.setInterval(collectLevelMeasurements, 0); } window.setInterval(calculateAverage, 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.