2 - Simple “Delay/Pause” Function
Here is a simple “Delay/Pause” function (EDIT - that appears to be blocking?.. will post a better option soon )
// Adjustable sleep/delay function
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
Usage is like this…
sleep(1000); // Wait a second before proceeding
And thanks to @zeeko for pointing me in the right direction with some code snippets.
Here is a silly little routine for Blinking a Virtual LED when the button is on.
There is probably much nicer ways of doing this, but it works and shows how an interval and timeout timer work.
const BlinkButton = new blynk.VirtualPin(26); // Setup Button Widget
const BlinkLED = new blynk.VirtualPin(27); // Setup LED Widget
var intervalId;
BlinkButton.on('write', function(param) { // Watches for virtual Blink Button
if (param == 1) { // If Button OFF
run();
} else if (param == 0) { // If Button ON
stop()
}
});
function run() {
intervalId = setInterval(blinkON, 1000); // Repeat every second
}
function stop() {
clearInterval(intervalId);
}
function blinkON() {
blynk.virtualWrite(27, 255); // Virtual LED OFF
setTimeout(blinkOFF, 500); // Run in 1/2 second
}
function blinkOFF() {
blynk.virtualWrite(27, 0); // Virtual LED ON
}