【发布时间】:2016-04-26 15:14:57
【问题描述】:
我有一个脚本,可以让我在画布中显示网络摄像头并在某些时间间隔内“下载”特定帧。 当时间参数很大(每 2 秒捕获 30 分钟)时,我遇到了麻烦。它可以顺利运行大约 15 分钟,然后崩溃(firefox 因内存不足错误而关闭)。此外,在重新启动 Firefox 后,有时会在 3-4 分钟内拍摄许多 0 字节的照片,然后重新开始工作。我在放置在实验室的旧 2 GB RAM 机器上运行它,有没有办法减少内存使用?
这是一段带有参数和函数realizarCapturas的代码。
我可以添加其余代码,但我认为要优化的部分应该是这个。
var frecuenciaComienzoCaptura = 1; // how long till next capture
var frecuenciaCaptura = 3; //seconds between photos
var duracion = 5; // amount of photos to capture
function realizarCapturas(){
var i = 1;
var interval = setInterval(function(){
if(i <= duracion){
context.drawImage(video, 0, 0, 640, 480);
var imagen = document.getElementById("imagen");
imagen.href = canvas.toDataURL("image/png");
var now = new Date();
var filename = formatNumber(now.getHours()) + "-" + formatNumber(now.getMinutes()) + "-" + formatNumber(now.getSeconds());
imagen.download = filename + ".png"; // Make sure the browser downloads the image
imagen.click(); // Trigger the click
i = i+1;
}else{
clearInterval(interval);
}
}, frecuenciaCaptura * 1000);
}
setInterval(function(){
realizarCapturas();
}, frecuenciaComienzoCaptura * 1000 * 60 * 60);
realizarCapturas();
}, false);
【问题讨论】:
-
您可能正在堆积未完成的操作。尝试使用单个
requestAnimationFrame循环而不是多个间隔进行重构。使用开发工具监控您的资源使用情况。
标签: javascript memory canvas out-of-memory setinterval