Last active
February 17, 2024 12:54
-
-
Save fvdm/23a77945799d204512877caf87aee9a2 to your computer and use it in GitHub Desktop.
HomeyScript to stall the flow a number of minutes, seconds or millieconds (default)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* HomeyScript to stall the flow a number of [m]inutes, [s]econds or [m]illi[s]econds (default) | |
* | |
* @example 2 -> waiting 2ms | |
* @example 2m -> waiting 2 min | |
* @example 2s -> waiting 2 seconds | |
* | |
* @see {@link https://gist.github.com/fvdm/23a77945799d204512877caf87aee9a2} | |
*/ | |
let ms = args[0]; | |
const regex = /^(?<amount>\d+) *(?<unit>ms|m|s)$/; | |
const match = regex.exec( ms ); | |
const units = { | |
'ms': 1, | |
's': 1000, | |
'm': 60000, | |
}; | |
if ( match ) { | |
ms = match.groups.amount * units[match.groups.unit]; | |
} | |
ms = parseInt( ms, 10 ) || 1000; | |
ms = ms < 0 ? 0 : ms; | |
await wait( ms ); | |
return true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment