Home > database >  How to disable multiple axios.post?
How to disable multiple axios.post?

Time:04-20

function executeStatement() {
    let time = global.setInterval(() => {
        var date = new Date();
        if (date.getHours() === 14 && date.getMinutes() === 38) {
            console.log("What did it choose?", result_shuffle);
            console.log("Time", time);
                if (shufflearray) {
                    var sql = `UPDATE developers SET selected = ${result_shuffle.selected}   1 WHERE id = ${result_shuffle.id}`;
                    db.query(sql, function (err, result) {
                        if (err) throw err;
                        console.log(result.affectedRows   " record(s) updated");
                    });
                    axios.post('https://hooks.slack.com/services/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', {
                        blocks: [
                            {
                                type: 'section',
                                text: {
                                    type: 'mrkdwn',
                                    text: `Name: yo \n\n Email: sup`,
                                },
                            },
                        ],
                    })   
                }
                else {
                    const abort = "abort"
                    console.log(abort);
                }
            }
        }, 2000);
};

So I'm building a function that executes a certain value when the timestamp 14:38 has hit. This message is then redirected to a Slack channel (Slack bot basically). Everything works smoothly BUT I'm having a problem. The axios.post is sending multiple POSTS.

So my slack channel is now being spammed with Name: Email: posts.

How can I make it so it only publishes once when the time hits? Or how can I make the time more precise so the axios.post will stop?

CodePudding user response:

You can modify your if statement something like this

if (date.getHours() === 14 && date.getMinutes() === 38 && date.getSeconds() < 2) {
// ....
}

This will check for seconds as well

  • Related