Skip to content

Instantly share code, notes, and snippets.

@leolanese
Created February 28, 2019 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leolanese/b531169c374ab61cc9c5ed9dcff6e5b0 to your computer and use it in GitHub Desktop.
Save leolanese/b531169c374ab61cc9c5ed9dcff6e5b0 to your computer and use it in GitHub Desktop.
The function creates a new function that adds a delay in milliseconds to the execution of another function that is passed as an argument.
function addDelay(func: () => void, ms: number) {
return () => {
setTimeout(() => {
func();
}, ms);
};
}
function sayHello() {
console.log('Hello world!');
}
const delayedSayHello = addDelay(sayHello, 600);
delayedSayHello(); // Prints "Hello world!" (after 600 ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment