Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Last active December 9, 2024 01:52
Show Gist options
  • Save luislobo14rap/c767b3371802828ed8a2538877e07d6a to your computer and use it in GitHub Desktop.
Save luislobo14rap/c767b3371802828ed8a2538877e07d6a to your computer and use it in GitHub Desktop.
set-timesout.ts
// setTimesout.ts v2.1
export function setTimesout(fn: (timeout: number, index: number) => void, repeats = [0]) {
for (let index = 0; index < repeats.length; index++) {
const currentTimeout = repeats[index]
setTimeout(() => {
fn(currentTimeout, index)
}, currentTimeout)
}
}
// setTimesout.js v2.1
function setTimesout(function_, repeats = [0]) {
if (typeof(function_) !== "function") {
throw new Error("The first argument must be of type 'function'")
}
if (!Array.isArray(repeats)) {
throw new Error("The second argument must be of type 'object' (array)")
}
repeats = repeats.sort((a, b) => {
return a - b
})
for (let i = 0; i < repeats.length; i++) {
setTimeout(() => {
function_(repeats[i], i)
}, repeats[i])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment