Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 内存泄漏-定期下载映像_Javascript_Jquery_Memory Leaks - Fatal编程技术网

Javascript 内存泄漏-定期下载映像

Javascript 内存泄漏-定期下载映像,javascript,jquery,memory-leaks,Javascript,Jquery,Memory Leaks,我做了一个代码: var camImage = camImage || {}; camImage.getImg = function() { var currDate = new Date(); var image = null; var link = 'http://localhost/picture.php?rand='+currDate.valueOf(); $.ajax({ url: link, success: func

我做了一个代码:

var camImage = camImage || {};
camImage.getImg = function() {
    var currDate = new Date(); 
    var image = null;
    var link = 'http://localhost/picture.php?rand='+currDate.valueOf();
    $.ajax({
        url: link,
        success: function(){
            $('#camera img').eq(0).remove();
            image = $('<img />').attr('src', link);
            $('#camera').append(image);
            link = null;
            image = null;
            currDate = null;
        }
    });

};
$(document).ready(function(){
    setInterval(camImage.getImg, 1000);
});
var camImage=camImage | |{};
camImage.getImg=函数(){
var currDate=新日期();
var-image=null;
var-link=http://localhost/picture.php?rand=“+currDate.valueOf();
$.ajax({
网址:link,
成功:函数(){
$('#摄像头img').eq(0).remove();
图像=$('
这段简单的代码定期获取新图像并将其添加到DOM中。

随着每次下载的图像浏览器内存使用量的增加。此代码是否会导致内存泄漏?

不需要Ajax请求。只需将img src设置为新URL即可:

camImage.getImg = function() {
    var src = 'http://localhost/picture.php?rand=' + (new Date()).valueOf();
    $('#camera img:first').detach().attr('src', src).appendTo('#camera');
}

我使用
detatch()
而不是
remove()
,因为这样可以保留原始DOM元素供重用。

Ajax请求是不必要的。只需将img src设置为新URL即可:

camImage.getImg = function() {
    var src = 'http://localhost/picture.php?rand=' + (new Date()).valueOf();
    $('#camera img:first').detach().attr('src', src).appendTo('#camera');
}

我使用
detatch()
而不是
remove()
,因为这样可以保留原始DOM元素以供重用。

更改函数并没有带来太多:

camImage.getImg = function() {
  var src = 'http://localhost/picture.php?rand=' + (new Date()).valueOf();
  $('#camera img:first').detach().attr('src', src).appendTo('#camera');
}

内存使用率仍在增加。这是由于javascript内存泄漏还是有其他原因?

更改函数没有带来太多:

camImage.getImg = function() {
  var src = 'http://localhost/picture.php?rand=' + (new Date()).valueOf();
  $('#camera img:first').detach().attr('src', src).appendTo('#camera');
}
内存使用仍在增加。这是由于javascript内存泄漏还是有其他原因