30秒学会 JavaScript 片段 · 2018年9月19日

30秒学会 JavaScript 片段 – delay

Invokes the provided function after wait 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, wait, ...args) => setTimeout(fn, wait, ...args);

使用样例

delay(
  function(text) {
    console.log(text);
  },
  1000,
  'later'
); // Logs 'later' after one second.