Having a error when I create a typewrite effect

Why the error shows me:

Uncaught TypeError: Cannot read properties of null (reading 'cut')
    at Frame.setSize (phaser.esm.js:209659:24)
    at Text.updateText (phaser.esm.js:81803:24)
    at Text.setText (phaser.esm.js:81198:18)
    at typeWriter (typewriter.js:31:26)

this is how I create a typewriting effect:


export class typewrite {
    callText(game, {
        text,
        x,
        y,
        style
    }) {
        let currentIndex = 0;
        let textOver = game.add.text(x, y, '', {
            fontSize: style.fontSize,
            fill: style.fontColor,
        });

        let textArray = Array.from(text);

        let txt = [];
        let i = 0;
        var speed = 50;
        let t = ""
        let timeout;
        let currentEvent;

        function typeWriter() {
            if (i < textArray.length) { // Use textArray.length instead of text.length
                t += textArray[i];
                i++;
                timeout = setTimeout(typeWriter, speed);
            }

            if (textOver) { // Check if textOver is not null
                textOver.setText(t);
            }
        }

        // Start typing when the Text object is created
        typeWriter();

        return { textOver, timeout };
    }
}

The output was okay but the error console still showed the error

When does this happen? Usually it’s because the Text game object has been destroyed but you’re still trying to update its contents.

Avoid setTimeout() for this. Use a timer event instead.