Cant add a wait or pause after a command runs


#1

I’m trying to get my program to wait like half a second after it runs IFTTT_EVENT_1. It will turn a button on then off on a remote so my lights will turn off and on. Only inserted part of the code.

// Loops through each event and where it finds a value for it in .env it will make a request to IFTTT using it
function checkForTrigger(trigger){
var triggerEvent;

if(trigger===0)
triggerEvent=process.env.IFTTT_EVENT_1;
if(trigger===1)
triggerEvent=process.env.IFTTT_EVENT_2;
if(trigger===2)
triggerEvent=process.env.IFTTT_EVENT_3;
if(trigger===3)
triggerEvent=process.env.IFTTT_EVENT_4;
if(trigger===4)
triggerEvent=process.env.IFTTT_EVENT_5;
if(trigger===5)
triggerEvent=process.env.IFTTT_EVENT_6;
if(trigger===6)
triggerEvent=process.env.IFTTT_EVENT_7;
if(trigger===7)
triggerEvent=process.env.IFTTT_EVENT_8;
if(trigger===8)
triggerEvent=process.env.IFTTT_EVENT_9;
if(trigger===9)
triggerEvent=process.env.IFTTT_EVENT_10;

if(triggerEvent){
// Make a request to baseURL + triggerEvent + withKey + iftttId, which is the complete IFTTT Maker Request URL
request(baseURL + triggerEvent + withKey + iftttId, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Show the response from IFTTT
}
});
}
}


Is it possible to add a timer to this
#2

You could use setTimeout() e.g.:

function waitFunction() {
        // all the stuff you want to happen after that pause
        console.log('this should be 0.5 seconds later');
}

// called after 0.5 seconds
setTimeout(waitFunction, 500);

Where you check if(triggerEvent) then add a further check for the trigger number and if it’s >1, call setTimeout(), with the request call within waitFunction().