我有一個腳本,允許我在畫布上顯示攝像頭,並在某些間隔內「下載」特定的幀。 當時間參數很大時(每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'循環而不是多個間隔進行重構。使用開發工具監視資源使用情況。 – markE