1

I have a setInterval function with no clearInterval, it is for an experimental glitch effect page that changes the background from time to time quite randomly.

My question is, would this cause a memory leak? or is there a way to improve it? that is, is there a way to make the interval run forever just as it is now, but without causing a memory leak? Right now I feel like the page is running very slow because of that.

This is my code (React JS)

imageTimerHandler() {
        this.setState({
            interval: setInterval(() => {
                this.randomGlitchZoomHandler();
                this.createGlitchRows();
                let newSeconds = this.state.seconds + .1;
                this.setSeconds(newSeconds);
                this.selectImageHandler(newSeconds);
            }, 100),
        });
    }
  • 1
    "*My question is, would this cause a memory leak?*" do you retain and accumulate objects objects? It doesn't seem like it, but I can't say for certain. Anyway, `setInterval` has been used for (essentially) infinite tasks for decades now. Simply using it is not going to cause a memory leak, depends on whether you keep adding to the memory. – VLAZ Nov 10 '20 at 16:24
  • You should probably check this out -> https://stackoverflow.com/a/14851513/854025 – Link Nov 10 '20 at 16:30
  • @VLAZ no, I don't, in fact the interval keeps overwritting some data to make it random, thanks! – Victor Shinobi Gakiya Nov 10 '20 at 16:38

0 Answers0