SetInterval with a variable a the timer

Hi,

This is a merge of two combining questions which would finish my project software wise!

First I’m running a script that gets a number from an api. I want to use that number = post.length and divide this with 9 minutes.
This because the outcome of the division should give me the seconds needed to send a message.

The message is an osc message based on a random math between 1 - 8.

So the “timer” (which is usually 1000 ms) should be the number of the outcome of 540 / post.length.
When the timer hits it should send the osc message with a random 1 - 8 number.

I know I’m probably asking a lot. But I’ve been struggling to long to find this on my own.

The code:

const soundtrigger = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8];
const randSoundtrigger = soundtrigger[Math.floor(Math.random() * soundtrigger.length)];

var  timer = (post.length / 540);

console.log(timer);

....

    console.log("Posts received with hashtag: ".cyan + posts.length);
    let promises = [];
    for (let i = 0; i < posts.length; i++) {
        let post = posts[i];

    }
    Promise.all(promises).then(function() {
        console.log("end");
    });
  }
// every 10 minutes do hashtag job
    schedule.scheduleJob('*/7 * * * *', async function () { await hashtag(ig)});

})();
setInterval(function() {
    var msg = {
        address: "/play/note",
        args: [
            {
                    // midi note value 
                        type: "i",
                        value: randSoundtrigger
            },
            {   // amp value a float to send decimal numbers
                type: "f",
                value: 1.5
            },
                {   // sustain value
                        type: "f",
                        value: 2
                    }
        ]
    };

    console.log("Sending message", msg.address, msg.args, "to", udpPort.options.remoteAddress + ":" + udpPort.options.remotePort);
    udpPort.send(msg);
}, timer);   =>>>>>> This timer value should be the result of post.length / 540

The const should probably be a for loop for each sent interval?
But I have no clue how to achieve this.

The timer I thought was simple by just dividing 540 by the post.length in a variable and then using it but I guess that’s not possible out of the function block??

Any help! Would very appreciated.

Kind regards!

I’ve read the code of conduct.

Only category I might think I am in is spamming.
But I explained this to another mod? And I rebuild the post completely.

I’m very sorry to seem spammy!

Any idea how to get the post.length out of the if statement and use it as the timer? I’m at a dead end.