Files
30-seconds-of-code/snippets/js/s/delay-function.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

548 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Delay function execution snippet javascript
function
interior-13 2020-10-19T18:51:03+03:00

Invokes the provided function after ms milliseconds.

  • Use setTimeout() to delay execution of fn.
  • Use the spread (...) operator to supply the function with an arbitrary number of arguments.
const delay = (fn, ms, ...args) => setTimeout(fn, ms, ...args);
delay(
  function(text) {
    console.log(text);
  },
  1000,
  'later'
); // Logs 'later' after one second.