setInterval javascript 内存泄漏

发布于 2024-11-15 20:09:37 字数 2506 浏览 2 评论 0原文

我不明白为什么内存会增加,并且每次运行此代码时它都会留在那里:

easingFunction = function (t, b, c, d) {
    if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
    return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
}
processFrame = function () {
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        tile.percent += 4;
        if (tile.percent > 0) {
            var TH = Math.max(0, Math.min(TILE_HEIGHT, targetObj.height - tile.imageY));
            var TW = Math.max(0, Math.min(TILE_WIDTH, targetObj.width - tile.imageX));
            var SW, SH, SX, SY, amount;
            draw.save();
            draw.translate(tile.imageX, tile.imageY);
            if (direction == "tb" || direction == "bt") {
                amount = easingFunction(tile.percent, 0, TW, 100);
                SW = Math.min(TW, amount);
                SH = TH;
                SX = 0;
                SY = 0;
            } else {
                amount = easingFunction(tile.percent, 0, TH, 100);
                SW = TW;
                SH = Math.min(TH, amount);
                SX = 0;
                SY = 0;
            }
            draw.drawImage(copycanvas, tile.imageX, tile.imageY, SW, SH, SX, SY, SW, SH);
            draw.restore();
        }
    }
    var ok = true;
    for (i = 0; i < tiles.length; i++) {
        if (tiles[i].percent < 100) {
            ok = false;
            break;
        }
    }
    if (ok) {
        clearInterval(interval);
        showComplete();
    }
};
this.show = function (target, hideTarget) {
    createTiles();
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        tile.percent = 0 - i * 10;
    }
}
var intervalDelay = (config.duration * 1000) / (tiles.length * 3 + 25);
interval = setInterval(function () {
    processFrame();
}, intervalDelay);
};

function Tile() {
    this.imageX = 0;
    this.imageY = 0;
    this.percent = 0;
};
};

我遗漏了一些不重要的代码。我的想法是从外部调用 show() 函数。 setInterval 被初始化并运行 processFrame() 大约 100 次。

我试图将一些代码留在 processFrame 之外,并且我必须:

processFrame = function () {
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        tile.percent += 4;
    }
    var ok = true;
    for (i = 0; i < tiles.length; i++) {
        if (tiles[i].percent < 100) {
            ok = false;
            break;
        }
    }
    if (ok) {
        clearInterval(interval);
        showComplete();
    }
};

但内存仍然增加。

I can't figure out why the memory is increasing and it stays there each time I run this code:

easingFunction = function (t, b, c, d) {
    if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
    return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
}
processFrame = function () {
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        tile.percent += 4;
        if (tile.percent > 0) {
            var TH = Math.max(0, Math.min(TILE_HEIGHT, targetObj.height - tile.imageY));
            var TW = Math.max(0, Math.min(TILE_WIDTH, targetObj.width - tile.imageX));
            var SW, SH, SX, SY, amount;
            draw.save();
            draw.translate(tile.imageX, tile.imageY);
            if (direction == "tb" || direction == "bt") {
                amount = easingFunction(tile.percent, 0, TW, 100);
                SW = Math.min(TW, amount);
                SH = TH;
                SX = 0;
                SY = 0;
            } else {
                amount = easingFunction(tile.percent, 0, TH, 100);
                SW = TW;
                SH = Math.min(TH, amount);
                SX = 0;
                SY = 0;
            }
            draw.drawImage(copycanvas, tile.imageX, tile.imageY, SW, SH, SX, SY, SW, SH);
            draw.restore();
        }
    }
    var ok = true;
    for (i = 0; i < tiles.length; i++) {
        if (tiles[i].percent < 100) {
            ok = false;
            break;
        }
    }
    if (ok) {
        clearInterval(interval);
        showComplete();
    }
};
this.show = function (target, hideTarget) {
    createTiles();
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        tile.percent = 0 - i * 10;
    }
}
var intervalDelay = (config.duration * 1000) / (tiles.length * 3 + 25);
interval = setInterval(function () {
    processFrame();
}, intervalDelay);
};

function Tile() {
    this.imageX = 0;
    this.imageY = 0;
    this.percent = 0;
};
};

I left out some unimportant code. The ideea is that I call externally the show() function. The setInterval is initialized and runs processFrame() about 100 times.

I've tried to leave some code outside from processFrame, and I got to :

processFrame = function () {
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        tile.percent += 4;
    }
    var ok = true;
    for (i = 0; i < tiles.length; i++) {
        if (tiles[i].percent < 100) {
            ok = false;
            break;
        }
    }
    if (ok) {
        clearInterval(interval);
        showComplete();
    }
};

But the memory still increases.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小瓶盖 2024-11-22 20:09:37

尝试使用 JSLint 验证您的代码。 http://www.jslint.com/

现在您添加了 easingFunction & processFrame 到 Global 对象(这不是一件好事)。这并不是问题的原因,但我发现对象管理不善是内存泄漏的常见原因。

您需要执行以下操作:

var MyObject = {}; 

MyObject.easingFunction = function(){};
MyObject.processFrame = function(){};

简而言之,请确保在使用所有对象之前使用 var 声明它们。

Try validating your code with JSLint. http://www.jslint.com/

Right now your adding easingFunction & processFrame to the Global object (which isn't a good thing). Not that this is the cause of the problem, but I've found that mismanagement of my objects is the usual cause of memory leaks.

You'll want to do something like:

var MyObject = {}; 

MyObject.easingFunction = function(){};
MyObject.processFrame = function(){};

In short make sure you declare all objects with var before using them.

森林很绿却致人迷途 2024-11-22 20:09:37

我发现了问题。我不断地重新绘制画布。为了解决这个问题,我每次修改画布之前都必须擦除它。

I found the problem. I was continuously redrawing the canvas. To resolve this problem I had to erase the canvas each time before modifying it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文