Flashing lights with Function Node

Trying to create a single function node that can provide an alternating output between on and off with a configurable delay. I can get the output to change but whenever I attempt to add delay, I get the `following from the debug node:

msg.payload : number
1639527910824

Here is my code:

for (let i=1; i<40; i++) {
    task(i);
    node.send(msg)
}

function task(i) {
    setTimeout(function() {
        if (i%2 == 0) {msg.payload = "on";}
        else {msg.payload = "off";}
    }, 2000 * i);
    return msg;
}