JavaScript setTimeout and Chrome memory utilization

894 views Asked by At

I am facing a problem with javascript and browser (Chrome) memory utilization.

There is timer (setTimeout) in my script, which is check location from browser. I have noticed that there is gradual increase in memory utilization (in task manger) by this tab which result in max memory among all tabs after some time and eventually page freezes and crashes.

Is there any way or some JS using which I can free memory after some time?

solution :

Before :

function recalculateDistance() {
    getLocation();
    getDistance();
    setTimeout("recalculateDistance()", 10000);
}

After :

var timer = null;

function recalculateDistance() {
        clearTimeout(timer);
        getLocation();
        getDistance();
        timer = setTimeout("recalculateDistance()", 10000);
}
1

There are 1 answers

1
Samuli Hakoniemi On BEST ANSWER

Post the code, as suggested.

There is clearTimeout() for, like the name says, clearing timeouts. This could help you?

And in addition, if you're doing a "timer", I'd suggest using setInterval() instead of setTimeout()