Beware with jquery animate in infinite loop Javascript caused memory leaks


After do Check memory leak javascript in Chrome, i found that animate and delay in Javascript quite consume memory. And it’s end up with memory leaks.

Here are my code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for (var i = 0; i < split; i++) {
    …

    $("tr.cmp_tr").css({
        ‘display’ : ‘none’
    });

    for (var k = 0; k < split; k++) {
        TimeOutObj = setTimeout(function() {
            $("tr#cmp" + k).css(‘opacity’, 1).animate({
                opacity : 1
            }, 1000);
        }, 100 * i);
    }

    var k = 0;
    TimeOutObjSecond = setTimeout(function() {
        k += 1;
    }, 100 * i);
}

Then i change this line :

1
2
3
4
5
for (var k = 0; k < split; k++) {
    TimeOutObj = setTimeout(function() {
        $("tr#cmp" + k).show();
    }, 100 * i);
}

Voila! Now memory still in 2.9MB 😀


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.